使用 Microsoft Excel 对象库 12.0 修改 Excel .xls 文件
我正在尝试打开 .xls 文件,并对某些单元格进行填充颜色。
使用此示例: http://csharp.net-informations.com/excel /csharp-format-excel.htm 我添加了以下行:“chartRange = xlWorkSheet.get_Range("b9", "e9");”
chartRange.Interior.ColorIndex = 6;
这给了我想要的细胞颜色。
然而,该示例正在制作一个 Excel 文件,并且在我的程序中我正在打开一个现有的 .xls 文件。我能够读取单元格中的值,因此我相信我可以访问它们,但是在我的代码执行并打开文件后,应该用黄色着色的单元格却没有,我还尝试更改中的文本单元格并且这些更改也没有保存。那么如何保存对文件所做的更改呢?
我还尝试使用此行在 .Close() 代码行之前执行 SaveAs...
xlWorkBook.SaveAs(
"C:\\sample.xls",
Excel.XlFileFormat.xlWorkbookNormal,
misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive,
misValue, misValue, misValue, misValue, misValue);
我用...打开文件...
xlWorkBook = xlApp.Workbooks.Open(
"C:\\sample.xls", 0, false, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t", false, false, 0, true, 1, 0);
我用...关闭文件
xlWorkBook.Close(true, misValue, misValue);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这篇帖子中接受的答案.
另一个参考可以是这个.
要选择一个范围,我通常使用:
Take a look at the accepted answer in this post.
Another reference could be this.
To select a range I usually use: