enter code here
我无法在excel表中写入数据。最初我将数据放在列表'ListTickets'中,然后将值分配给列表中的每个单元格。但是它不起作用我得到了没有数据的空白表。
public static void UpdateIncidentExcel(string Incident_Path,List<excelObj> ListTickets)
{
// lastRow = 0;
MyApp = new Excel.Application();
// MyApp.Visible = false;
MyBook = MyApp.Workbooks.Open(Incident_Path);
MySheet = (Excel.Worksheet)MyBook.Sheets["BOXI_summary"]; // Explict cast is not required here
//lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
int j=(ListTickets.Count() == 1) ? 3 : (ListTickets.Count() == 2) ? 4 : ListTickets.Count();
for (int i = 2; i < j; i++)
{
int k = i - 2;
MySheet.Cells[i, 1] = ListTickets[k].Bundle_Name;
MySheet.Cells[i, 2] = ListTickets[k].Incident_Number;
MySheet.Cells[i, 3] = ListTickets[k].Status_Symbol;
MySheet.Cells[i, 4] = ListTickets[k].Aging;
MySheet.Cells[i, 5] = ListTickets[k].Incident_Area;
MySheet.Cells[i, 6] = ListTickets[k].Service_Call_Type;
MySheet.Cells[i, 7] = ListTickets[k].Assigned_To_Workgroup;
MySheet.Cells[i, 8] = ListTickets[k].Assigned_To_Name;
MySheet.Cells[i, 9] = ListTickets[k].Assignee_LAN_ID;
MySheet.Cells[i, 10] = ListTickets[k].Open_Date;
MySheet.Cells[i, 11] = ListTickets[k].Close_Date;
MySheet.Cells[i, 12] = ListTickets[k].Resolve_Date;
MySheet.Cells[i, 13] = ListTickets[k].Last_Modified_Date;
MySheet.Cells[i, 14] = ListTickets[k].Application_ID;
MySheet.Cells[i, 15] = ListTickets[k].AMS_Application_Name;
MySheet.Cells[i, 16] = ListTickets[k].Tier;
MySheet.Cells[i, 17] = ListTickets[k].Priority_Symbol;
MySheet.Cells[i, 18] = ListTickets[k].Application_Class;
MySheet.Cells[i, 19] = ListTickets[k].Contains_PHI_SPI;
MySheet.Cells[i, 20] = ListTickets[k].Reach_Back_Required;
MySheet.Cells[i, 21] = ListTickets[k].Parent_Number;
MySheet.Cells[i, 22] = ListTickets[k].Vendor_Engagement_Date;
MySheet.Cells[i, 23] = ListTickets[k].Vendor_Response_Date;
MySheet.Cells[i, 24] = ListTickets[k].Problem_Number;
MySheet.Cells[i, 25] = ListTickets[k].Caused_By_Change_Order_Number;
MySheet.Cells[i, 26] = ListTickets[k].Change_Number;
MySheet.Cells[i, 27] = ListTickets[k].Hold_Date;
MySheet.Cells[i, 28] = ListTickets[k].Reason_Dependency;
MySheet.Cells[i, 29] = ListTickets[k].Resolution_Next_Update_Date;
MySheet.Cells[i, 30] = ListTickets[k].Resolution_Method_Description;
MySheet.Cells[i, 31] = ListTickets[k].Resolution_Code_Description;
MySheet.Cells[i, 32] = ListTickets[k].Hours_of_Availability;
MySheet.Cells[i, 33] = ListTickets[k].Core_Hours_Mon_Fri;
MySheet.Cells[i, 34] = ListTickets[k].Core_Hours_Saturday;
MySheet.Cells[i, 35] = ListTickets[k].Core_Hours_Sunday;
}
MyBook.Save();
MyBook.Close(0);
MyApp.Quit();
// Call the garbage collector to collect and wait for finalizers to finish.
GC.Collect();
GC.WaitForPendingFinalizers();
// Release the COM objects that have been instantiated.
Marshal.FinalReleaseComObject(MyBook);
Marshal.FinalReleaseComObject(MySheet);
Marshal.FinalReleaseComObject(MyApp);
}
}
答案 0 :(得分:0)
只是一个友好的FYI,您正在使用的Excel互操作对象仅适用于客户端/桌面应用程序。 Microsoft不支持或建议在服务器上使用任何Office互操作(自动化)对象。它们效率很低,可能存在安全问题并且经常导致严重的内存泄漏。
https://support.microsoft.com/en-us/kb/257757
我尝试了一些不同的策略来在服务器上创建Excel文件,我最喜欢的是在响应中以编程方式创建Excel XML文件。它速度快,重量轻,使您能够进行所需的所有格式化。唯一的缺点是客户端必须手动将其保存为.xlsx才能获得所有Excel功能。