I am creating a function to read from excel. And this is my code
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Integer, ByRef lpdwProcessId As IntPtr) As IntPtr
...
...
Dim oExcelFormula As Excel.Application
Dim oBookFormula As Excel.Workbook
Dim processId As IntPtr
Dim excelProcess As Diagnostics.Process
strReturn = 'C:\Test.xls'
oExcelFormula = New Excel.Application()
GetWindowThreadProcessId(oExcelFormula.Hwnd, processId)
excelProcess = Diagnostics.Process.GetProcessById(processId.ToInt32())
oBookFormula = oExcelFormula.Workbooks.Open(strReturn, 0, True)
And when it try to execcure this line, it always return error:
The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.COMException: The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
//i dont know vb but i know c#.easily you can change your code c# to vb..
using System;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
calss Program
{
public static void Main(string []ar)
{
Microsoft.Office.Interop.Excel._Application _xlApplication = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook _xlWorkbook = _xlApplication.Workbooks.Open("c:\\a.xls");
Microsoft.Office.Interop.Excel._Worksheet _xlWorksheet = _xlWorkbook.Sheets[1];
Excel.Range xlRange = _xlWorksheet.UsedRange;
Object[,] xlRecordArray = (System.Object[,])(xlRange.Cells.Value2); // insert into array
_xlWorkbook.Close();
_xlApplication.Quit();
}
}
You are trying to use desktop Excel from web application. It is not supported scenario and with all errors and problems you get you are alone. I suggest you to use Office OpenXML library to work with Excel files on server. On API side this library is not
very easy to use at first place as it is heavily targeted to XML of Office documents. But you can do most of stuff using this library and you don't need any complex communication between different processes.
Don't forget to mark solution providing post as "Answered".
ryanlcs
Member
134 Points
457 Posts
Unable to read from excel
Jan 11, 2013 04:57 AM|LINK
Hi
I am creating a function to read from excel. And this is my code
And when it try to execcure this line, it always return error:
Error message is as such:
Any one can help?
Thanks.
aaa_78600
Participant
844 Points
177 Posts
Re: Unable to read from excel
Jan 11, 2013 08:26 AM|LINK
//i dont know vb but i know c#.easily you can change your code c# to vb.. using System; using System.Linq; using System.Text; using System.Data; using System.IO; using Excel = Microsoft.Office.Interop.Excel; calss Program { public static void Main(string []ar) { Microsoft.Office.Interop.Excel._Application _xlApplication = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook _xlWorkbook = _xlApplication.Workbooks.Open("c:\\a.xls"); Microsoft.Office.Interop.Excel._Worksheet _xlWorksheet = _xlWorkbook.Sheets[1]; Excel.Range xlRange = _xlWorksheet.UsedRange; Object[,] xlRecordArray = (System.Object[,])(xlRange.Cells.Value2); // insert into array _xlWorkbook.Close(); _xlApplication.Quit(); } }DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: Unable to read from excel
Feb 05, 2013 09:37 PM|LINK
You are trying to use desktop Excel from web application. It is not supported scenario and with all errors and problems you get you are alone. I suggest you to use Office OpenXML library to work with Excel files on server. On API side this library is not very easy to use at first place as it is heavily targeted to XML of Office documents. But you can do most of stuff using this library and you don't need any complex communication between different processes.
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman