|
|
Comments and Discussions
|
|
 |

|
Excepción de HRESULT: 0x8002000B (DISP_E_BADINDEX))
Para los que tengamos configurado como espaniol se debe cambiar:
Sheet1 por Hoja1
|
|
|
|

|
Thank you so much for solution, i am looking for it.
|
|
|
|

|
excelApp.Visable = true;
You meant to spell it as Visible.
|
|
|
|

|
Whoops, that is odd because I thought I just copied and pasted the code. It is correct in the projects, and I just fixed it in the article. Thanks for the catch.
|
|
|
|
|

|
the author didn't mention about how to manipulate cells!
|
|
|
|

|
it is almost excellent, i had a lot of troubles while implementing the incidents . so 4 star.
|
|
|
|

|
HI,
Any body knows how to bold a part of a excel cell content with c# coding? eg: abcdefgh if so plz send a email to supuna2u@gmail.com or post the answer here. This is a big help! I have googled but could not came up with a solution
Thank you
|
|
|
|

|
How to open an excel workbook which is of type SPListItem from "Shared Documents" of a sharepoint site through C# code?
the code below is used to open a workbook located in our local machine:
Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open("c:\..\somefile.xls", 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
In case of shared documents, which pathname do we have to specify?
|
|
|
|

|
I got the following error:
COMException was unhandled
Old format or type of library not valid.
Exception HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)
I assume that this error is because I'm using Excel 2007
Thxs for the help
Hugo Mota
|
|
|
|

|
I am not sure where you got this error, but it is most likely due to the fact you are using Excel 2007. The best advice I can give you is to look at examples for VBA and remember that none of the parameters in the c# methods are optional.
|
|
|
|

|
HI
I have an excel file to be read in C#. I used the ODBC to read the data from the excel and it works fine. But one of its sheets has embeded objects e.g Textbox in it. I have to read data from that text box. So how can i read this text box data from C#.
|
|
|
|

|
Hi,
I am a C++ developer and wanted to just open an existing excl file from c#. Its a huge relief.
Thanks a million.
|
|
|
|

|
Hi
I have downloaded the source files. They don't appear to do anything.
|
|
|
|

|
The code, when set-up will open a Excel file, go to sheet1 and in Cell A1, will write Hi There. Now, there are a couple of things to do before this code works. First, you need to go to the references section and make sure you are referencing the correct version of Excel, I believe the code as is is referencing Office XP (this is older code). Second, you need to make sure you change the path to an existing Excel file (replace c:/SomeWorkBook.xls with a real file). It should run fine from there.
|
|
|
|

|
Windows XP Service Pack 3
Microsoft Office 2003 SP3
error:
Microsoft.Office.Interop.Excel.WorkbookClass wkb = (Microsoft.Office.Interop.Excel.WorkbookClass)app.Workbooks.Open(File, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, false, Missing.Value);
How to run application in Windows XP Service Pack 2 is work!
|
|
|
|

|
I've looking everywhere for this.
Thank you so much!
=)
Battlecry: Archer the GREAT !!!
|
|
|
|

|
I have an Excel workbook made up of several sheets. The first sheet contains cells that populate common fields throughout the other sheets.
What I am trying to do is populate pre-defined fields on the first sheet with information from a database and triggering this from a Web page.
Obviously the Web server does not have Excel installed, but you can assume the client machine does.
How do I go about trying to achieve this?
Thanks in advance!
|
|
|
|

|
I am pretty sure that the server would have to have Excel installed on it since all of the code behind your page would be running on the server.
|
|
|
|

|
Hi,
using Excel Interop is not recommended on server, even by Microsoft.
I suggest you try some 3rd party alternative like this Excel C# / VB.NET library.
Here is a sample how to use it in your Excel ASP.NET app:
DataTable people = (DataTable)Session["people"];
ExcelFile ef = new ExcelFile();
ExcelWorksheet ws = ef.Worksheets.Add("DataSheet");
ws.InsertDataTable(people, "A1", true);
Response.Clear();
switch (this.RadioButtonList1.SelectedValue)
{
case "XLS":
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename="
+ "Report.xls");
ef.SaveXls(Response.OutputStream);
break;
case "XLSX":
Response.ContentType = "application/vnd.openxmlformats";
Response.AddHeader("Content-Disposition", "attachment; filename="
+ "Report.xlsx");
MemoryStream ms = new MemoryStream();
ef.SaveXlsx(ms);
ms.WriteTo(Response.OutputStream);
break;
}
Response.End();
|
|
|
|

|
Hi,
I have tried the code (and also other examples found on the internet) but all are resulting in a COM Exception 0x80028018 (TYPE_E_INVDATAREAD) when trying to open the excel sheet.
Here is the complete error message:
------------------------------
System.Runtime.InteropServices.COMException was unhandled
Message="Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))"
Source="Interop.Excel"
ErrorCode=-2147319784
StackTrace:
at Excel.Workbooks.Add(Object Template)
at ExcelExample.ExcelClass.Main(String[] args)
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
-----------------------------------
I'm using Excel 2003 SP3 (11.8169.8172) with Visual Studio 2005 (8.0.50727.762 - SP.050727-7600) and .NET Framework V2.0.50727 SP1
Any suggestions?
Thanks a lot
Thorsten
|
|
|
|

|
Unfortunately I am unable to re-create the problem, but here is a suggestion that will hopefully help:
In the project I supplied it has some outdated references, so try adding the new reference from the Project->Add References... menu choice. Go to the COM tab and choose the most current Microsoft Excel library. Then go to the References folder under the solutions explorer and remove the Excel and Microsoft.Office.Core references. If you get some compile errors like Excel is not a valid reference or something like that, put this into the using directives:
using Excel = Microsoft.Office.Interop.Excel;
I am wondering if the the Excel and Microsoft.Office.Core references are causing some conflicts.
I hope this helps.
|
|
|
|

|
Solution from http://www.gotdotnet.ru/Forums/Common/290167.aspx[^]
Before creating Excel object
System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
After closing Excel:
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
|
|
|
|
|

|
tanks a lottttttttttttttttt
|
|
|
|

|
I want same feature like Ctrl + End, which takes me to the last avaialable blank row in an Active WorkSheet. Please help me
|
|
|
|

|
You need to use the .SpecialCells method.
I have not tested this just yet, but here it goes. To get to the last cell in the last column you can use this:
Where curCell is a an Excel.Range object.
curCell.SpecialCells(xlCellTypeLastCell, System.Type.Missing).Select
Here is the method signature:
.SpecialCells(Type, Value)
The values for Type and Value are listed below.
Type Required XlCellType. The cells to include.
XlCellType can be one of these XlCellType constants.
xlCellTypeAllFormatConditions. Cells of any format
xlCellTypeAllValidation. Cells having validation criteria
xlCellTypeBlanks. Empty cells
xlCellTypeComments. Cells containing notes
xlCellTypeConstants. Cells containing constants
xlCellTypeFormulas. Cells containing formulas
xlCellTypeLastCell. The last cell in the used range
xlCellTypeSameFormatConditions. Cells having the same format
xlCellTypeSameValidation. Cells having the same validation criteria
xlCellTypeVisible. All visible cells
Value Optional Variant. If Type is either xlCellTypeConstants or xlCellTypeFormulas, this argument is used to determine which types of cells to include in the result. These values can be added together to return more than one type. The default is to select all constants or formulas, no matter what the type. Can be one of the following XlSpecialCellsValue constants:
XlSpecialCellsValue can be one of these XlSpecialCellsValue constants.
xlErrors
xlLogical
xlNumbers
xlTextValues
|
|
|
|

|
Hi
Need help!
I have 2 excels,I am comparing the 1st with 2nd.
If a row in 1st excel is not found in 2nd excel, then I need to insert the row from 1st excel to 2nd excel.how do i do it?
|
|
|
|

|
U can use StreamReader and StreamWritter.........
|
|
|
|

|
Hi, I have a huge and very complex spreadsheet some of the rows locked, hidden, merged, grouped, with formulae all over, protected (and what not!); that I'm supposed to read within C#. I had some initial problems doing this with the password protection, COM errors etc.. that I could resolve. Now I'm having an unusual problem.. I read a range of values using 'a while loop' and 'get_range' as follows: object m; m = System.Reflection.Missing.Value; //first time string strPMSheetField = "BN"; string strPMCurRow = "6"; objPMRange = (Excel.Range)excelPMWorksheet.get_Range(strPMSheetField + strPMCurRow, m); if (objPMRange.Value2 != null) strName = objPMRange.Value2.ToString(); else strName = ""; //loop down the rows while (strName != "") { //add the name to my array list etc. // //set next row no. strPMCurRow = Convert.ToInt32(strPMCurRow) + 1; //read data objPMRange = (Excel.Range)excelPMWorksheet.get_Range(strPMSheetField + strPMCurRow, m); if (objPMRange.Value2 != null) strName = objPMRange.Value2.ToString(); else strName = ""; } What is happening is until some rows it gets the value fine, but at some specific rows, it skips reading the current row and gets the values in the following cell (row). Not sure, but I'm guessing that some rows are 'locked'/'hidden' under a specific logic and the get_range when it comes across such 'locked' field is simply going to the next one. ??? I couldn't find that specific logic anywhere in spreadsheet to hide/lock but I'm assuming it. In the debug it always shows 'hidden=false', 'locked=false' or says that property 'threw exception' My Trials: 1. I tried to set these before I read the value but in vain: excelPMWorksheet.get_Range(strPMSheetField + strPMCurRow, m1).Locked = false; excelPMWorksheet.get_Range(strPMSheetField + strPMCurRow, m1).EntireRow.Locked = false; excelPMWorksheet.get_Range(strPMSheetField + strPMCurRow, m1).EntireRow.Hidden = false; 2. I tried to read the whole range BN6:BO136 into a 2-dim Array, the result is the same: If there are 50 rows only 46 would show up and 4 are skipped. 3. I wrote a VBA Module to see if it happens there - it worked fine there (no rows were skipped).
Please help.. I'm lost.. -ap
|
|
|
|

|
I am not sure what is going on, there must be some strange permissions on the cells. I tried it with just a sample sheet with some rows hidden and some rows locked and it got every value. I have tried just about everything I can think cell formatting wise to get it to skip some rows with no luck. The only suggestion that I have is to take a hard look at the rows that are giving you problems and check out their formatting. Sorry to not be any more help.
|
|
|
|

|
after I finished working with a workbook, should I "kill" the worksheet object or leave it to the garbage collector ?
in order to close the workbook what overloads I have to load?
excelWorkbook.Close(___,____,____); ?
I'm almost finished working on my homework thanks to you
(i have to copy the results to dataset and to show a chart, but i know how to do it)
Thanks to you and to THE CODE PROJECT !!!!
|
|
|
|

|
excelWorkbook.Close(bool,bool,bool);
I coulnd understand it at first because in the msdn instead of "bool" (true/false) was written "object so I couldnt understand what they ment to say.....
sometimes it's very frastrating to code but I guess this was the easiest "problem" to handle.
|
|
|
|

|
hi I'm new in programming...
and where can I see all the procedures in the Excel class ?
thanks a lot !!!
|
|
|
|

|
Here is a link the all of the objects in the Excel Namespace: http://msdn.microsoft.com/en-us/library/bb149081.aspx[^]
They are pretty much the same functions that you can find in VBA within Excel, so you can look at the object model in VBA for just about the same thing.
|
|
|
|

|
OK. I saw that there are excel 2003 documentation (VBA) and 2007(the link in your answer).
I found what I wanted but there are only vb examples... When I tried to convert it to c# I got errors...
all I need to do is to copy a range of columns from one workbook to anoter through c#.
can you advice me about this?
http://msdn.microsoft.com/en-us/library/bb178833.aspx
http://msdn.microsoft.com/en-us/library/bb178854.aspx
http://msdn.microsoft.com/en-us/library/bb210384.aspx
Worksheet.Copy Method
http://msdn.microsoft.com/en-us/library/bb179176.aspx
Worksheet.Paste Method
http://msdn.microsoft.com/en-us/library/bb179200.aspx
With Worksheets("Sheet1")
.Range("C1:C5")
.Copy.Range("D1:D5")
.PasteSpecial _
Operation:=xlPasteSpecialOperationAdd
End With
When I tried to convert from vb to c# i got this :
CONVERSION ERROR: Code could not be converted. Details:
-- line 3 col 75: invalid EndOfStmt
Please check for any errors in the original code and try again.
thank you very much.
|
|
|
|

|
Here is an Example of how you could do it:
Excel.Range destination = (Excel.Range)excelWorksheet.get_Range("C1", "C4");
excelWorksheet.get_Range("A1", "A4").Copy((Excel.Range)destination);
This takes the Contents of A1:A4 and copies them over to C1:C4. Hope this helps.
|
|
|
|

|
Thanks a LOT !!!!!!!
this is how I did it (with your assistance, of course)
Excel.Application excelApp = new Excel.ApplicationClass();
excelApp.Visible = true;
string workbookPath = "e:/111.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
string workbookPath2 = "e:/222.xls";
Excel.Workbook excelWorkbook2 = excelApp.Workbooks.Open(workbookPath2, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Sheets excelSheets2 = excelWorkbook2.Worksheets;
string currentSheet2 = "Sheet1";
Excel.Worksheet excelWorksheet2 = (Excel.Worksheet)excelSheets2.get_Item(currentSheet2);
Excel.Range destination = (Excel.Range)excelWorksheet2.get_Range("C1", "D9");
excelWorksheet.get_Range("A1", "B9").Copy((Excel.Range)destination);
by the way, is copying thousands of columns is fast with this Excel Engine ?
or in that case I sould use third party components(like Infragistics Excel Engine) ?
thanks again, by the way how did you learn to work with this Excel class ?
|
|
|
|

|
Hi,
I am using the above code in Visual studio 2005, but I keeping getting Com Exception error
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
any suggestions whats going wrong?
Thanks
/M
|
|
|
|

|
That is odd, when are you getting the exception, at Compile Time? I have used the code in VS 2005 and have not had any problems. I assume you have Excel installed on the machine you are getting the exception from.
|
|
|
|

|
I am getting error at runtime on this line
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
|
|
|
|

|
Here is the exception detail:
System.Runtime.InteropServices.COMException was unhandled
Message="Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))"
Source="Interop.Excel"
ErrorCode=-2147352565
StackTrace:
at Excel.Sheets.get_Item(Object Index)
at ExcelExample.ExcelClass.Main(String[] args) in C:\Documents and Settings\Manu\My Documents\Visual Studio 2005\Projects\ExcelExample\ExcelClass.cs:line 38
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
|
|
|
|

|
I just played with it a little in VS 2005 and did not have a problem. Are you sure that the Sheet you are trying to access exists with the name you are using? Also what version of Excel are you using, I have tested this on XP and 2003 formats.
|
|
|
|

|
I am using VS 2005, and I used Excel 2003 when I had this exception.
Now I am using Excel 2007 and having issues with libraries.
How to use this code for Excel 2007?
|
|
|
|

|
For the using statement I had to do this:
using Excel = Microsoft.Office.Interop.Excel;
There is one other line you will have to change which is switching XlWBATemplate.xlWBATWorksheet to Excel.XlWBATemplate.xlWBATWorksheet.
Hope this helps.
|
|
|
|

|
When I have a date cell in excel such as 3/15/2008
When I read it into C# string, I get the value as 39527.
How to Convert into datetime?
If I use Convert.ToDateTime(decimal/string/double..), I'm getting an invalid cast exception.
Please help
Thanks
-ap
|
|
|
|

|
Use this function to convert the serial date (Excel returns the days since 1/1/1900):
DateTime GetExcelDate(int serialDate)
{
int l = serialDate + 68569 + 2415019;
int n = (int)(( 4 * l ) / 146097);
l = l - (int)(( 146097 * n + 3 ) / 4);
int i = (int)(( 4000 * ( l + 1 ) ) / 1461001);
l = l - (int)(( 1461 * i ) / 4) + 31;
int j = (int)(( 80 * l ) / 2447);
int day = l - (int)(( 2447 * j ) / 80);
l = (int)(j / 11);
int month = j + 2 - ( 12 * l );
int year = 100 * ( n - 49 ) + i + l;
return new DateTime(year, month, day);
}
Now Excel will return a double so 3/15/2008 is actually 39527.0. The decimal part is for the time. So if you need a time instead of just the date, you will have to modify the code a bit. I did get the algorithm to do this here: http://www.codeproject.com/KB/datetime/exceldmy.aspx[^]
It seems that Excel fixed the 2/29/1900 problem, that the article mentioned, so do not worry about that anymore.
|
|
|
|

|
Thanks a lot! This helped!
|
|
|
|

|
Thanks a lot...GRAZIE MILLE (in Italian)
|
|
|
|

|
i am automating my formatted excel work book. now i want to first retreive the last filled row of an already existing excel sheet and then want to fill my array day next to that row. please help me ASAP. its bugging me.
shrads
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
Introduction to manipulating Excel with C#.
| Type | Article |
| Licence | CPOL |
| First Posted | 2 Oct 2003 |
| Views | 975,096 |
| Downloads | 14,328 |
| Bookmarked | 221 times |
|
|