| | Author | |
| | Date | |
|
|
|
check Excel97 language setting |
| | Uffel3 | 9:56 1 Jun '05 |
| I want to automate an Excel97 application using C#. I use the excel8.olb file as COM interface. How can I check the country code of the installed Excel application? I know that there is a method "LanguageSettings.get_LanguageID()", but it works with Excel XP.
regards |
| [Modify|Delete] |
|
|
|
|
|
| | ro_angel_bv | 3:22 30 May '05 |
| Hi,
First of all I'd like to say that you've done a good job with this article.Congrats!
I wrote an app to export some custom data to Excel and I have a small but annoying problem. The security policy of the network I'm testing within is not allowing Excel to be started from my app.
WOuld you have any solution on how to solve this from my program, I mean creating an over all portable application ?
Thanks.
Angel |
| [Modify|Delete] |
|
|
|
|
|
| | seeker@seekerspalace.com | 10:19 28 May '05 |
| Hi, I'm trying to create a reference to the Excel 10 Object Library but I can't find it in the reference list. I have Office XP installed so it should be there? Any ideas? | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | seeker@seekerspalace.com | 10:23 28 May '05 |
| oh, I found it my self. It was called Microsoft Excel 10 Object Library... Clever me... |
| [Modify|Delete] |
|
|
|
|
|
| | Anonymous | 19:20 5 May '05 |
| How can I read data from Excel using C#? I am able to use get_range function to read data from a cell, However, I do not know the number of columns and rows in an Excel worksheet and without knowing the number of columns and rows, I can not read data from the whole worksheet. | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | yevaud_us | 20:58 6 May '05 |
| Anonymous wrote: How can I read data from Excel using C#? I am able to use get_range function to read data from a cell, However, I do not know the number of columns and rows in an Excel worksheet and without knowing the number of columns and rows, I can not read data from the whole worksheet.
Same here. If I figure it out I will let you know.
Seems like you should be able to just do something like:
foreach ( Excel.Worksheet sheet in workbook.Sheets ) { sheet.Activate(); foreach (Excel.Range row in sheet.Rows) { Console.WriteLine("Do something with row: {0}.", row.ToString()); } }
but this results in an error indicating that the .Rows property is not defined.
I have had a similar problem when trying to use the .Cells property of the sheet.
Help would be appreciated on this.
-Michael
yevaud_us@yahoo.com | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | yevaud_us | 21:11 6 May '05 |
| Anonymous wrote: How can I read data from Excel using C#? I am able to use get_range function to read data from a cell, However, I do not know the number of columns and rows in an Excel worksheet and without knowing the number of columns and rows, I can not read data from the whole worksheet.
Same here. If I figure it out I will let you know.
Seems like you should be able to just do something like:
foreach ( Excel.Worksheet sheet in workbook.Sheets ) { sheet.Activate(); foreach (Excel.Range row in sheet.Rows) { Console.WriteLine("Do something with row: {0}.", row.ToString()); } }
but this results in an error indicating that the .Rows property is not defined.
I have had a similar problem when trying to use the .Cells property of the sheet.
Help would be appreciated on this.
-Michael
yevaud_us@yahoo.com | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | Jeff Ruys | 23:28 8 May '05 |
| To get the address of the last cell, which you coul use to get the number of rows and columns use this bit of code:
string cellAddress;
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");
excelCell.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Excel.XlSpecialCellsValue.xlTextValues).get_Address(false, false, Excel.XlReferenceStyle.xlA1, false, CurCell);
The first parameter of the SpecialCells method points to the last cell, the second parameter says to go to the last cell that contains text.
The first parameter of the get_Address method is to determine if you want an absolute reference to the row ($ sign in front), the second parameter is if you want an absolute reference to the column, the third parameter is if you want the address formatted in "A1" (which is the code above) or R1C1 references to the Range object, the forth parameter is for an external reference, and the final parameter is needed for the code to compile, but is really only used if you want to use the R1C1 choice for the third parameter. |
| [Modify|Delete] |
|
|
|
|
| | Yevaud_us2 | 14:44 9 May '05 |
|
Jeff Ruys wrote: To get the address of the last cell, which you coul use to get the number of rows and columns use this bit of code:
Hmmm... unfortunately that doens't work for me Jeff. Perhaps I was using it wrong. Also, I was not sure what CurCell was supposed to be... it was undefined when I tried to use it so I created a Excel.Range variable for it, but I was not sure if that was what was expected.
Anyway, the resulting value of excelCell.Rows and exceCell.Columns were 1 and 1 respectively.
Drat!
All is not lost, though, at least for me. I found in a different place some code that does seem to work for me. It uses the UsedRange attribute of the sheet.
public void LoadTableFileInterop( string filePath ) { //... code deleted... int rowCount = 0; // how many rows we parsed int columnCount = 0;
rowCount = sheet.UsedRange.Rows.Count; columnCount = sheet.UsedRange.Columns.Count;
for(int r = 1; r < rowCount; r++) { for(int c=1; c < columnCount; c++) { string text = (string) ((Excel.Range)sheet.Cells[r,c]).Text; if ( text.Length > 0) { Console.WriteLine("Cell[{0},{1},\"{2}\"] = {3}.", r,c, cellAddress, text); } } } // ... code deleted ... }
|
| [Modify|Delete] |
|
|
|
|
| | Yevaud_us2 | 16:34 9 May '05 |
| oopps -- that writeline won't work because cellAddress is not defined. It was just a string Ic reated that showed the address in standard "B3" "A4" etc... format.
| [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | Jeff Ruys | 18:37 9 May '05 |
| I am sorry, CurCell should have been "excelCell". That was from some other test code I just copied and pasted in. The other way you just posted may be a better way of finding the number of rows and columns anyway. | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
|
| | Uffel3 | 9:11 26 Apr '05 |
| Hello,
I've got a problem with access to special ranges. I renamed some cells (for example "C4" in "CUSTOM.GENERAL.VPRIM"). This is necessary, because I have to read out only these cells in active worksheet. The method get_range("RangeName", Missing.Value) is ok as far as I have an existing RangeName.
My program compares the root of a customized XML file with the range of an Excel file. But some root names don't exist in the Excel file. If I deliver a wrong RangeName to the get_range() Method I get an Exception. So, I have to check the existance of a RangeName at first.
Can anybody help me? I've searched for 3 days in all forums without luck.
Many Thanks - Maik |
| [Modify|Delete] |
|
|
|
|
| | Jeff Ruys | 18:44 26 Apr '05 |
| I think the only way you are going to be able to check if a range exists is to put it into an error checking block. If an execption is thrown, you know the range does not exist and you can ignore it and move on to the next range and value. | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | Uffel3 | 4:16 27 Apr '05 |
| Hello Jeff,
thanks for replying me. To add a try - catch block is a good idea. I remember that it was possible in VBA to check for existing range names.
Sub FindRangeName() Dim n As Name For Each n In ActiveWorkbook.Names If n.Name = "CUSTOM.GENERAL" Then MsgBox "Name " & n.Name & " exist, refers to cell " & n.RefersToRange.Address End If Next End Sub
The Office primary interop assemblies don't offer this alternative in C# code. | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | Jeff Ruys | 0:00 28 Apr '05 |
| The interop assemblies do seem to have the Name and Names objects, but for some reason I can't get a foreach statement to work, it does not think that a Name object is part of the Names collection. I also tried using the Names.item(), but unlike VBA, it needs all three parameters. Here is the code I tried, maybe you can figure out how to get it to work better.
Excel.Application app = new Excel.ApplicationClass(); Excel.Workbook WB = app.Workbooks.Open(path, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
foreach (Excel.Name in WB.Names) { if (name.Name == "CUSTOM.GENERAL") { MessageBox.Show("Name " + name.Name + " exists, refers to cell " + name.RefersToRange); } } | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | Uffel3 | 3:37 28 Apr '05 |
| Hello Jeff,
last week, I tried to debug your shown code, but I get a "System.Runtime.InteropServices.COMException" exception. Here is a cutout of my code:
Excel.Application excelApp = null; Excel.Workbook workBook = null; System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture; object missing = Missing.Value;
private bool StartExcel() { try { if(excelApp == null) { excelApp = new Excel.ApplicationClass(); excelApp.Visible = true;
// Workaround to prevent a known bug int nId = excelApp.LanguageSettings.get_LanguageID( Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDInstall); System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(nId); return true; } else { MessageBox.Show("Excel has been started"); return false; } } catch (Exception theException) { Debug.WriteLine(theException.Message); return false; } }
private bool OpenWorkbook(string filename) { try { workBook = excelApp.Workbooks.Open(filename,0,false,5,"","",false,Excel.XlPlatform.xlWindows, "",true,false,0,true,false,false); return true; } catch { Debug.WriteLine("Could not open Workbook!"); return false; } } void ReadFromSheet() { Excel.Worksheet sheet; Excel.Range range; sheet = (Excel.Worksheet) workBook.ActiveSheet; string rangeName = "CUSTOM.GENERAL";
try { foreach(Excel.Name name in workBook.Names) { if(name.Name == "CUSTOM.GENERAL") { MessageBox.Show("Name" + name.Name + "exist, refers to cell" + name.RefersToRange); range = sheet.get_Range(rangeName,missing); range.Select(); } } } catch(System.Runtime.InteropServices.COMException e) { Debug.WriteLine(e.Message); } catch(System.NullReferenceException e) { Debug.WriteLine(e.Message); } catch(System.Exception e) { Debug.WriteLine(e.Message); }
With start of "foreach(Excel.Name name in workBook.Names)" I get an exception. Why? The range name "CUSTOM.GENERAL" exists in every Worksheet. Is it possible to check the range name in my active worksheet? | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | Uffel3 | 16:49 15 May '05 |
| The code is now running. Thanks for help.
Best regards
private void getRangeValue() { Excel.Name name; this.myParameters = new Hashtable(); try { //search for range names in selected worksheet for(int i = 0; i< sheet.Names.Count ; i++) { name = sheet.Names.Item(i+1,missing,missing); //only range names refering to one cell and featuring a value will be added to hashtable if(sheet.get_Range(name.Name, missing).Value2 != null) { if(sheet.get_Range(name.Name, missing).Value2.ToString() != "System.Object[,]") { //save parameters in global hashtable string key = name.Name.Replace("'", ""); this.myParameters.Add(key,sheet.get_Range(name.Name, missing).Value2); } //end if } //end if } //end for } //end try
catch(System.Runtime.InteropServices.COMException caught) { MessageBox.Show("Fehler beim Zugriff auf benannte Zellbereiche im aktuellen " + "Tabellenblatt. Folgende Fehlermeldung wurde generiert:" + Environment.NewLine + Environment.NewLine + caught.Message, "Zugriffsfehler", MessageBoxButtons.OK, MessageBoxIcon.Hand); }
catch(Exception caught) { MessageBox.Show("Es ist ein Fehler aufgetreten. Folgende Fehlermeldung wurde " + "generiert" + Environment.NewLine + Environment.NewLine + caught.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Hand); }
finally { // clean up name = null; } }
| [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | Jeff Ruys | 0:19 16 May '05 |
| I am glad that you found out how to get that .Item method to work, I couldn't figure out how to get that one to work. It is too bad that the foreach does not recognize the .Names as a collection, it probably would have been a lot easier. | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
|
| | amyat | 4:09 14 Feb '05 |
| hi,
i used the same code to create an excel file from .cs(console application) and aspx.cs (web application) .
But there is a "Server Eexcution failed" error when i reach the following statement in web application.
this.objApp = new Excel.ApplicationClass();
can anyone help me how to resolve it?
thks,
| [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
|
| | Anonymous | 11:41 10 Feb '05 |
| Hi,
My excel sheet looks something like
Item1 Item2 Item3 1 2.2$ 2.2$ 3.4$ 2 1.9$ 3.2$ 3 6.2$
and so on..
How can I set/get a cell at position [Item1,3] ???? I dont want to access it as actual cell number of excel sheet like A1 etc
THanks | [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | Flora PL | 6:36 12 Apr '05 |
| Have You tried sth like this?
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); excel.Application.Workbooks.Add(true); for (int RowIndex = 1; RowIndex < 10; RowIndex++) for (int ColumnIndex = 1; ColumnIndex < 10; ColumnIndex++) excel.Cells[RowIndex,ColumnIndex] = (char)(64 + ColumnIndex) + RowIndex.ToString(); excel.Visible = true;
| [Reply][Email][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
|
| | Anonymous | 12:30 26 Jan '05 |
| .Net programmer in C# trying to change the format of an Excel collumn to Text.
This makes the data in column D display in cash format whenever new data is entered in col D (oRng is the Excel.Range object, oSheet):
oRng = (Excel.Range)oSheet.Columns["D",Type.Missing]; oRng.NumberFormat = "$0.00";
How do I make column E always display as text format? The problem is that some of the data has leading 0's and I need them preserved.
Thanks,
Heri | [Reply][View Thread][Get Link] | [Modify|Delete] |
|
|
|
|
| | Jeff Ruys | 18:45 26 Jan '05 |
| Hi Heri,
If you want to set the cell to a text format use the following: oRng.NumberFormat = "@";
If you know the number of leading zeroes then you can use: oRng.NumberFormat = "$0000.00"; Just put however many zeroes in front of the decimal point that you need. I believe Excel only uses this for display purposes though and if you wanted to compare 001 and 01, they would be the same.
Hope this helped, Jeff |
| [Modify|Delete] |
|
|
|
|
|
| | Chau Duong | 22:42 9 Jan '05 |
|
Can write code C++ in C#? |
|
|
|
|
| | Jeff Ruys | 22:56 9 Jan '05 |
|
I am not quite sure what you are talking about? I am not all that familiar with C++, so I am not sure I can be of too much help. |
|
|
|
|