Exception from HRESULT:0x800A03EC如何修改??

lubaixs 2014-07-21 03:52:07
在用模板导出excel时报错:Exception from HRESULT:0x800A03EC
具体的错误代码是:xBk = workbooks.Add(excelTemplateDPath);
各位大牛帮下忙。。。。实在不知道哪里的问题

GC.Collect();
string excelTemplateDPath = templatepath; //EXCEL模板默认服务器物理存放路径
Excel.Application excel = null;
Excel._Workbook xBk = null;
Excel._Worksheet xSt = null;
Excel.Workbooks workbooks; //工作簿集合
Excel.Sheets sheets; //SHEET页集合
try
{
#region
//启动excel进程并加载模板
//启动EXCEL进程
excel = new Excel.Application();
excel.EnableEvents = false;
excel.Visible = false;
excel.UserControl = true;
excel.DisplayAlerts = false;
//加载读取模板
workbooks = excel.Workbooks;
insertintoTest(excelTemplateDPath);
xBk = workbooks.Add(excelTemplateDPath);
sheets = xBk.Worksheets;

#endregion

#region output2
if (dt2.Rows.Count > 0)
{

xSt = (Excel._Worksheet)sheets["supply plan product view"];

int rowCount = 0;
int colCount = 0;
//保存数据
//表头
rowCount = Head2.Rows.Count;
colCount = Head2.Columns.Count;

object[,] dataArray = new object[colCount, rowCount];

for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
dataArray[j, i] = Head2.Rows[i][j];
}
}
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[colCount, rowCount]).Value2 = dataArray;
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[colCount, rowCount]).Font.Name = "Arial";
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[colCount, rowCount]).Font.Size = 11;
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[colCount, rowCount]).Font.Bold = true;
xSt.Columns.AutoFit();
//数据

rowCount = dt2.Rows.Count;
colCount = dt2.Columns.Count;

object[,] dataArray1 = new object[rowCount, colCount];
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
dataArray1[i, j] = dt2.Rows[i][j];
}
}
xSt.get_Range(xSt.Cells[2, 1], xSt.Cells[rowCount + 1, colCount]).Value2 = dataArray1;
xSt.get_Range(xSt.Cells[2, 1], xSt.Cells[rowCount + 1, colCount]).Font.Name = "Arial";
xSt.get_Range(xSt.Cells[2, 1], xSt.Cells[rowCount + 1, colCount]).Font.Size = 10;

//将excel的列宽设置为固定长度
xSt.get_Range(xSt.Cells[2, 1], xSt.Cells[rowCount + 1, colCount]).ColumnWidth = 13;
//单元格边框
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[rowCount + 1, colCount]).Borders.LineStyle = 1;

}
#endregion
object objOpt = System.Reflection.Missing.Value;
excel.Visible = false;

xBk.SaveCopyAs(absFileName);
// (Microsoft.Office.Interop.Excel.Worksheet)xBk.Close(false,null,null);
xBk.Close(false, null, null);

excel.DisplayAlerts = true; //注意一定要加上这句
excel.EnableEvents = true;
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;

}
catch (Exception e)
{
throw e;
}
...全文
341 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lubaixs 2014-07-27
  • 打赏
  • 举报
回复
恩恩,是文件乱码了,嘿嘿 害我找了好久
BrightFireOfCy 2014-07-22
  • 打赏
  • 举报
回复
Workbooks.Add method Workbook Add( Object Template ) Template Type: System.Object Optional Object. Determines how the new workbook is created. If this argument is a string specifying the name of an existing Microsoft Excel file, the new workbook is created with the specified file as a template. If this argument is a constant, the new workbook contains a single sheet of the specified type. Can be one of the following XlWBATemplate constants: xlWBATChart, xlWBATExcel4IntlMacroSheet, xlWBATExcel4MacroSheet, or xlWBATWorksheet. If this argument is omitted, Microsoft Excel creates a new workbook with a number of blank sheets (the number of sheets is set by the SheetsInNewWorkbook property). 你传入的string里面的路径上的文件可能不存在,或者无权访问。
lubaixs 2014-07-21
  • 打赏
  • 举报
回复
各位有会的吗,帮个忙撒

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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