一直报说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: 异常来自 HRESULT:0x800A03EC
代码如下:protected void btnCreate_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Word.Application word = null;
Microsoft.Office.Interop.Word.Document doc=null; object SaveChanges = true; //保存更改
object OriginalFormat = System.Type.Missing;
object RouteDocument = System.Type.Missing;
try
{
string path = Server.MapPath("Doc/");//目录
string infoListXLS = path + "myexcel.xls";//EXCEL模版
string wordTemplate = path + "template.doc";//WORD模版
excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook = excel.Workbooks.Open(infoListXLS, System.Type.Missing, false, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
//Microsoft.Office.Interop.Excel.Worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
excel.Visible = false; int intRow = 2;
//string repStr = "&Chr(13)&Chr(10)";
//string desStr = "@"; //开始循环,直到第十一列“题干”为空时退出循环
while (excel.Cells[intRow, 11].ToString() != "")
{ //取得第十一列内容即 题干
string ti = excel.Cells[intRow, 11].ToString();
//取得第十二列内容即 内容,实现把@替换成Word中的换行符
string daan = excel.Cells[intRow, 12].ToString();
string docFile = path + ti + ".doc";
if (!File.Exists(docFile))
{
File.Copy(wordTemplate,docFile);
word = new Microsoft.Office.Interop.Word.Application();
word.Visible = false;
object fileName = docFile; //文件名称 object ConfirmConversions = false; //允许转换 object ReadOnly = false; //只读方式打开 object AddToRecentFiles = false; //添加到最近打开的文档 object PasswordDocument = System.Type.Missing; object PasswordTemplate = System.Type.Missing; object Revert = System.Type.Missing; object WritePasswordDocument = System.Type.Missing; object WritePasswordTemplate = System.Type.Missing; object Format = System.Type.Missing; //格式 object Encoding = System.Type.Missing; //编码 object Visible = System.Type.Missing; object OpenAndRepair = System.Type.Missing; object DocumentDirection = System.Type.Missing; object NoEncodingDialog = System.Type.Missing; object XMLTransform = System.Type.Missing; doc = word.Documents.Open(ref fileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);
Microsoft.Office.Interop.Word.Table docTable = doc.Tables[0];
docTable.Cell(1, 1).Range.Text = ti;
docTable.Cell(2, 1).Range.Text = daan;
doc.Save();
doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
word.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
intRow++;
}
excel.Quit(); }
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
if (word != null)
{
word.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
excel.Quit();
} }
代码如下:protected void btnCreate_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Word.Application word = null;
Microsoft.Office.Interop.Word.Document doc=null; object SaveChanges = true; //保存更改
object OriginalFormat = System.Type.Missing;
object RouteDocument = System.Type.Missing;
try
{
string path = Server.MapPath("Doc/");//目录
string infoListXLS = path + "myexcel.xls";//EXCEL模版
string wordTemplate = path + "template.doc";//WORD模版
excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook = excel.Workbooks.Open(infoListXLS, System.Type.Missing, false, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
//Microsoft.Office.Interop.Excel.Worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
excel.Visible = false; int intRow = 2;
//string repStr = "&Chr(13)&Chr(10)";
//string desStr = "@"; //开始循环,直到第十一列“题干”为空时退出循环
while (excel.Cells[intRow, 11].ToString() != "")
{ //取得第十一列内容即 题干
string ti = excel.Cells[intRow, 11].ToString();
//取得第十二列内容即 内容,实现把@替换成Word中的换行符
string daan = excel.Cells[intRow, 12].ToString();
string docFile = path + ti + ".doc";
if (!File.Exists(docFile))
{
File.Copy(wordTemplate,docFile);
word = new Microsoft.Office.Interop.Word.Application();
word.Visible = false;
object fileName = docFile; //文件名称 object ConfirmConversions = false; //允许转换 object ReadOnly = false; //只读方式打开 object AddToRecentFiles = false; //添加到最近打开的文档 object PasswordDocument = System.Type.Missing; object PasswordTemplate = System.Type.Missing; object Revert = System.Type.Missing; object WritePasswordDocument = System.Type.Missing; object WritePasswordTemplate = System.Type.Missing; object Format = System.Type.Missing; //格式 object Encoding = System.Type.Missing; //编码 object Visible = System.Type.Missing; object OpenAndRepair = System.Type.Missing; object DocumentDirection = System.Type.Missing; object NoEncodingDialog = System.Type.Missing; object XMLTransform = System.Type.Missing; doc = word.Documents.Open(ref fileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);
Microsoft.Office.Interop.Word.Table docTable = doc.Tables[0];
docTable.Cell(1, 1).Range.Text = ti;
docTable.Cell(2, 1).Range.Text = daan;
doc.Save();
doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
word.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
intRow++;
}
excel.Quit(); }
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
if (word != null)
{
word.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
excel.Quit();
} }
解决方案 »
- c#基础的东西
- 往Lable上累加内容怎么不把前边添加的内容覆盖掉
- 请问C#winform 能否嵌套wpf的窗口呢 或者wpf自定义控件
- 紧急救助,DataGridView列绑定问题,有可能实现我要求的吗?
- 怎么把登入界面时的 用户名 ,在后来的存储中,一起存入 数据库
- web开发,点击GridView
- 请问如果要做一个点对多点的无线教学(语音)系统,可否用c#编写上位机程序,如何编写?
- 数据库中读出来的值的类型的问题。急!!!
- 求教正则表达式如何引用分组捕获的结果再进行匹配,谢谢
- 水晶报表问题,不能实例化?搞了一天了!(请海波.net等来看看)
- 如何用c#控制Microsoft Office PowerPoint 文件在保存和打开时自动设置密码和填写密码?
- BeginRead 阻塞
这一行就要出现这个异常
http://topic.csdn.net/u/20090617/11/4a48b4ca-b568-457c-8b1c-58da6270cb2a.html
Posted on 2009-12-21 12:05 阿松 阅读(2292) 评论(2) 编辑 收藏 所属分类: 其它, 原贴 好不容易花了半天终于解决了导出数据到Excel出现“异常来自 HRESULT:0x800A03EC”的错误。网上的解决方案基本上都是说开始行的下标值写成了0导致的“excel.Cells[0, 1] = "第1行第1列";”,改为大于0就能解决。但我的程序都是从第4行开始写数据的,想可能不是这个问题导致的。插入断点,调试程序,又出现“不能单步执行”的错误。 后来又仔细看了这个异常,发现异常是从“excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);”这个语句跳出来的,如果我把写入数据行的小标从0开始,异常会从那个写入行时跳出来。发现问题可能是在这个语句的参数上,于是随便把语句改为了“excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel7, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);”居然发现解决了问题,这个问题可能是我的Excel的版本不对吧(我的excel版本为2007)。信息来源:http://www.cnblogs.com/Gaton/articles/1628761.html
再就是了再给CEll复制的时候是先行号,在列好。
而且你看一下你的赋值是不是格式正确了。必须是 值类型
{
for ( i = 0; i < 26; i++) //xlsSheet.Cells.Columns.Count
{
if (i == 8)
{
xlsSheet.get_Range(xlsSheet.Cells[j + 1, i + 1], xlsSheet.Cells[j + 1, i + 1]).NumberFormatLocal = "0";//"G/通用格式" @ General
xlsSheet.get_Range(xlsSheet.Cells[j + 1, i + 1], xlsSheet.Cells[j + 1, i + 1]).Interior.ColorIndex = 18;
} str = str + "," + xlsSheet.Cells[i+1, j+1].ToString().Trim();
}
}