用C#编程Excel [英] Programming Excel in C#

查看:121
本文介绍了用C#编程Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择Col F-Col I中的所有数据行
并将数字格式更改为小数点后两位.
以下代码不起作用.有人可以帮我吗?
代码可以编译,但会因COM异常而崩溃(我无法弄清楚)


I want to select all the data rows in Col F - Col I
and change the number format to have 2 decimal places.
The following code does not work. Can someone help me out?
The code compiles, but blows up with a COM exception(which I can''t figure out)


Excel.Range workSheet_range = sheet.get_Range("F", "I");
workSheet_range.NumberFormat = "0.00";

推荐答案

我认为您在此行中有例外:
I think you are having the exception in this line:
Excel.Range workSheet_range = sheet.get_Range("F", "I");


尝试将代码更改为应遵循的代码:


Try changing your code to following it should work:

using Excel;
using System.Reflection; // Needed for Missing.Value


// Code to get to the point of selecting the range
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook workbook = excelApp.Workbooks.Open("c:\yourexcelfile.xls", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Worksheet worksheet = (Excel.Worksheet) workbook.Worksheets.get_Item("sheet1");
// Select a multi column range
Excel.Range dataRange =worksheet.get_Range("F:F,I:I", Missing.Value);



还可以看看这两个链接:
链接1 [链接2 [



Also have a look at these two links:
Link 1[^]
Link 2[^]
It will help you solve your problem.

Good luck,
OI


这篇关于用C#编程Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆