开发者

Excel .xls File Modifying Using Microsoft Excel Object Library 12.0

I am trying to open a .xls file, and do a fill color on some cells.

Using this example: http://csharp.net-informations.com/excel/csharp-format-excel.htm I added this line following: "chartRange = xlWorkSheet.get_Range("b9", "e9");"

chartRange.Interior.ColorIndex = 6;

and this gave me the desired coloring of the cells.

The example is making a excel file however, and in my program I am opening an existing .xls file. I am able to read the values in the cells so I believe I can access them, but after my code executes and I open the file, the cells that should be colored in yellow are not, I also tried to just change the text in a cell and these changes were not saved aswell. So how do I save the changes I am making to the file?

I also tried this line to do a SaveAs before the .Close() line of code...

xlWorkBook.SaveAs(
    "C:\\sample.xls", 
    Excel.XlFileFormat.xlWorkbookNormal, 
    misValue, misValue, misValue, misValue, 
    Excel.XlSaveAsAccessMode.xlExclusive, 
    misValue, misValue, misValue, misValue, misValue);

I open the file with...

xlWorkBook = xlApp.Workbooks.Open(
    "C:\\sample.xls", 0, false, 5, "", 开发者_高级运维"", true,
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, 
    "\t", false, false, 0, true, 1, 0);

I close the file with...

xlWorkBook.Close(true, misValue, misValue);


Take a look at the accepted answer in this post.
Another reference could be this.
To select a range I usually use:

Excel.Range dataRange =
    worksheet.get_Range("A:A,D:D,F:F", Missing.Value); // Multicolumn
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜