向Excel中写入数据,SaveAs()抛出异常:异常来自 HRESULT:0x800A03EC

st_song_tao 2011-11-22 02:17:47

public static void GenerationExcel(List<List<string>> values)
{
string path = Utils.BasePath + "报表模板/通用报表模板.xls";
string newFilePath=Utils.CopyFile(path,"不知道叫什么.xls");
Office.XlFileFormat version = Office.XlFileFormat.xlExcel9795; //OFFICE 2003
if (path.LastIndexOf("xlsx") > -1) //OFFICE 2007
{
version = Office.XlFileFormat.xlExcel2;
}
Office.Application excel = new Office.Application();
excel.Visible = false;
excel.Application.Workbooks.Add(true);
Office.Workbook wb = excel.Workbooks._Open(newFilePath,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
Office.Worksheet sheet = (Office.Worksheet)wb.Sheets[1];
int currentRowNum = 1;
foreach (List<string> list in values)
{
int currentCellNum = 1;
foreach (string item in list)
{

Office.Range cell = (Office.Range)sheet.Cells[currentRowNum, currentCellNum];
cell.Value2 = item;
Console.WriteLine(Missing.Value+"====值====" + item + "===行===" + currentRowNum + "====列====" + currentCellNum);
currentCellNum++;
}
currentRowNum++;
}
excel.DisplayAlerts = false;
try
{
sheet.SaveAs(newFilePath, version, null, null, null, null, null, null, null, null);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

sheet = null;
wb = null;
excel.Quit();
excel = null;
}
...全文
359 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jinjazz 的回复:]
现在第三方的类库都很好用,比如Aspose.Net。
[/Quote]
剪剪出没~

@st_song_tao

用第三方的好一些。
阿非 2011-11-23
  • 打赏
  • 举报
回复
嗯,我之前常用的也是NPOI
st_song_tao 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 sandy945 的回复:]
引用 1 楼 jinjazz 的回复:
现在第三方的类库都很好用,比如Aspose.Net。

剪剪出没~

@st_song_tao

用第三方的好一些。
[/Quote]
关于这个Aspose.Net对各个版本的.Framework支持不是很好,昨天已经用NPOI完成了,建议用NPOI
biancheng1000 2011-11-22
  • 打赏
  • 举报
回复
使用流向Excel文件中写是不是好点呢,不过设置Excel格式方面费劲点。需要用XML语言去设置格式。
shaoxingss 2011-11-22
  • 打赏
  • 举报
回复
学习了,希望能和大家多交流,以后常来往
http://hi.csdn.net/space.html
st_song_tao 2011-11-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pnljh 的回复:]
最好还是用NPOI,快速,又不依赖本机office
[/Quote]

java 我经常用POI C#也可???
st_song_tao 2011-11-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 findcaiyzh 的回复:]
0x800A03EC 比较难判断是什么错误。

google下有可能是下面的原因
1. 没有权限写文件
2. 安装的Excel和Excel Com wrapper的版本不匹配
3. 安装的Excel和Excel Com wrapper的Cultue不匹配
4. 没有权限操作com 组件(感觉这个不像,因为如果没有权限, new Office.Application的时候就有问题了。)
[/Quote]
- -! 好像都不是引发这个问题的原因
st_song_tao 2011-11-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qldsrx 的回复:]
sheet.SaveAs的参数列表里不能使用null,全部用Type.Missing替代即可。
[/Quote]
哦,这个代码是我改过之后贴上来的 Missing.value 是一样的
qldsrx 2011-11-22
  • 打赏
  • 举报
回复
sheet.SaveAs的参数列表里不能使用null,全部用Type.Missing替代即可。
宝_爸 2011-11-22
  • 打赏
  • 举报
回复
0x800A03EC 比较难判断是什么错误。

google下有可能是下面的原因
1. 没有权限写文件
2. 安装的Excel和Excel Com wrapper的版本不匹配
3. 安装的Excel和Excel Com wrapper的Cultue不匹配
4. 没有权限操作com 组件(感觉这个不像,因为如果没有权限, new Office.Application的时候就有问题了。)
pnljh 2011-11-22
  • 打赏
  • 举报
回复
最好还是用NPOI,快速,又不依赖本机office
st_song_tao 2011-11-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jinjazz 的回复:]
现在第三方的类库都很好用,比如Aspose.Net。
[/Quote]
- -! 还有第三方啊,好久没做C#了,好我现在就试试看去,微软的API挺难用的
jinjazz 2011-11-22
  • 打赏
  • 举报
回复
现在第三方的类库都很好用,比如Aspose.Net。

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧