声明:本人新用户,没什么分,还请不吝赐教小弟在导出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_colRes");
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", "Res" }; 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]];
}
}
}
代码大致如下:
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_colRes");
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", "Res" }; 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]];
}
}
}
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吧?
郁闷啊,困扰了这么久......