导出Excel错误 "Exception from HRESULT: 0x800A03EC"

libra10062619 2011-02-25 02:07:28
声明:本人新用户,没什么分,还请不吝赐教

小弟在导出Excel时遇到错误HRESULT: 0x800A03EC,现象很奇怪,错误也不是每次都出现,只在某列数据长度过长(其实还没有超过30个字符长度)时会发生,将该数据截断后再导出,则没有问题。

另外,按照客户提供的现象去调试,没有发现任何问题。将所有需要导出的数据修改成数据库定义的最大长度也没有这个问题。部署到自己的测试服务器中,仍然能够正常导出。


代码大致如下:
Excel.Application oExcel = null;
Excel.Workbooks oBooks = null;
Excel.Workbook oBook = null;
Excel.Sheets oSheets = null;
Excel.Worksheet oSheet = null;
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
try
{
oExcel = new Excel.Application();
oBooks = oExcel.Workbooks;
oBook = oBooks.Add(System.Reflection.Missing.Value);
oSheets = oBook.Sheets;

IList<OrderDetailsEntity> orderList = 从数据库中获取数据;

DataTable dt = Paxar.GOS.Utility.PubFun.ConvertListToTable<OrderDetailsEntity>(orderList);//将list转换成DataTable
oSheet = this.getExcelSheet(oBook, oSheets, i);//获取sheet
fillSheet1(oSheet, dt);
filePath = this.Server.MapPath("..\\..\\Resource\\Report\\").ToString() + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";

oBook.SaveAs(filePath, Excel.XlFileFormat.xlWorkbookNormal, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlExclusive, Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
}
catch (Exception ex)
{
throw ex;
}
finally
{
oBook.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
oBooks.Close();
oExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
oSheet = null;
oSheets = null;
oBook = null;
oBooks = null;
oExcel = null;
System.GC.Collect();
GC.WaitForPendingFinalizers();
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
}

private void fillSheet1(Excel.Worksheet oSheet, System.Data.DataTable dt)
{
if (dt.Rows.Count < 1) return;
oSheet.Select(true);
oSheet.Name = "Shipping Detail";
string[] captions = new string[26];
captions[0] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colOrderNumber");
captions[1] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colLineNumber");
captions[2] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colRetailerPO");
captions[3] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colPONumber");
captions[4] = ResourceManagerWrapper.Instance.Get("Order_Status_g_shipFromSite"); //"Ship From Site";
captions[5] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colRetailer");
captions[6] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colBillTo");
captions[7] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colShipTo");
captions[8] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colOrderDate");
captions[9] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colPaxarItemNumber");
captions[10] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colCustomerItemNumber");
captions[11] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colItemDesc");
captions[12] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colPaxarSite");
captions[13] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colQtyOrdered");
captions[14] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colQtyShipped");
captions[15] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colRequestedShipDate");
captions[16] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colPromiseDate");
captions[17] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colScheduledShipDate");
captions[18] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colExFactoryDate");
captions[19] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colDateShipped");
captions[20] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colTrackingNumber");
captions[21] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colShipVia");
captions[22] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colInvoiceNumber");
captions[23] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colInvoiceDate");
captions[24] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colOrderStatus");
captions[25] = ResourceManagerWrapper.Instance.Get("Order_Status_g_colRemarks");
string[] columnNames = new string[] { "OrderNumber", "LineNumber", "RetailerPO", "PONumber", "ShipFromDesc", "Retailer", "BillTo", "ShipTo", "OrderDate", "PaxarItemNumber", "CustomerItemNumber", "ItemDesc", "PaxarSite", "QtyOrdered", "QtyShipped", "RequestedShipDate", "PromiseDate", "ScheduledShipDate", "ExFactoryDate", "DateShipped", "TrackingNumber", "ShipVia", "InvoiceNumber", "InvoiceDate", "OrderStatus", "Remarks" };

string[] formatStrings = new string[] { "", "", "", "", "", "", "", "", "yyyy-MM-dd", "", "", "", "", "#0.00", "#0.00", "yyyy-MM-dd", "yyyy-MM-dd", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss", "", "", "", "yyyy-MM-dd HH:mm:ss", "", "" };

for (int i = 0; i < columnNames.Length; i++)
{
oSheet.Cells[1, i + 1] = captions[i];
}
System.Collections.Generic.List<string> columnNames1 = new List<string>();

for (int i = 0; i < columnNames.Length; i++)
{
columnNames1.Add(columnNames[i]);
}
int rowCount = dt.Rows.Count + 1;
Excel.Range rg = oSheet.get_Range(oSheet.Cells[2, 1], oSheet.Cells[rowCount, columnNames1.Count]);
object[,] objData = new Object[dt.Rows.Count, columnNames1.Count];
fillRange(dt, objData, columnNames1);
rg.Value2 = objData;

rg.EntireColumn.AutoFit();
#endregion

for (int i = 0; i < formatStrings.Length; i++)
{
if (!formatStrings[i].Equals(string.Empty))
{
oSheet.get_Range(oSheet.Cells[2, i + 1], oSheet.Cells[dt.Rows.Count + 1, i + 1]).Select();

oSheet.get_Range(oSheet.Cells[2, i + 1], oSheet.Cells[dt.Rows.Count + 1, i + 1]).NumberFormat = formatStrings[i];
}
else
{
oSheet.get_Range(oSheet.Cells[2, i + 1], oSheet.Cells[dt.Rows.Count + 1, i + 1]).NumberFormat = "@";
}
}

oSheet.get_Range(oSheet.Cells[1, 1], oSheet.Cells[1, columnNames.Length]).Font.Bold = true;

......
}



private void fillRange(DataTable dt, object[,] objData, System.Collections.Generic.List<string> columnNames)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
DataRow row = dt.Rows[j];
for (int i = 0; i < columnNames.Count; i++)
{
if (dt.Columns[columnNames[i]].DataType.ToString().Equals("System.DateTime"))
{
DateTime dtime = DateTime.Parse(row[columnNames[i]].ToString());

if (dtime.Equals(DateTime.MinValue) || dtime.Equals(DateTime.MaxValue))
{
continue;
}
}
objData[j, i] = row[columnNames[i]];
}
}
}
...全文
532 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
libra10062619 2011-03-07
  • 打赏
  • 举报
回复
今天终于拿到客户的数据了,原来其中有一个本该是字符串的数据内容为“=RSN SHIPMENT=”,导入的时候,excel将其作为自身的函数操作了。

去掉“=”或者,objData[j, i] = "'" + row[columnNames[i]].toString();就解决了。
郁闷啊,困扰了这么久......
libra10062619 2011-02-28
  • 打赏
  • 举报
回复
经过不懈的调试,发现问题出在
Excel.Range rg = oSheet.get_Range(oSheet.Cells[2, 1], oSheet.Cells[rowCount, columnNames1.Count]);
object[,] objData = new Object[dt.Rows.Count, columnNames1.Count];
fillRange(dt, objData, columnNames1);
rg.Value2 = objData;
的最后一句“rg.Value2 = objData;”上。objData中有一个值长超过911,但是这个数据被限定为最长1000,总不能不允许用户输入长度超过911吧?
libra10062619 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]

http://topic.csdn.net/u/20091026/11/aaf75715-d274-48b2-ab7c-96a699297842.html
[/Quote]

这个我看过,不过我的实在是没法跟踪代码,本地调试和本地服务器部署都没有问题。
就客户那边出问题。

不过还是谢谢了。
yalan 2011-02-25
  • 打赏
  • 举报
回复
代码太长了,不好意思~~~~

110,571

社区成员

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

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

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