我目前使用Interop对象向Excel电子表格中写入内容。通过使用计时器,我发现以下实现相当及时。 在foreach
循环中的过程通常需要大约50秒来写入大约2000行,每行有9列。
有没有什么方法可以加快速度呢?
List<string[]> allEntries = fillStringArrayList();
// Set-up for running Excel
xls = new Excel.Application();
workBooks = xls.Workbooks;
workBook = workBooks.Open(workbookPath);
var workSheet = workBook.Sheets["Sheet1"];
// Insert new entries
foreach (string[] entry in allEntries)
{
// Get the final row in the sheet that is being used
Excel.Range usedRange = workSheet.UsedRange;
int rowCount = usedRange.Rows.Count;
// Format Column A to be type "text"
workSheet.Cells[rowCount + 1, 1].NumberFormat = "@";
workSheet.Cells[rowCount + 1, 1] = entry[0];
workSheet.Cells[rowCount + 1, 2] = entry[1];
workSheet.Cells[rowCount + 1, 3] = entry[2];
workSheet.Cells[rowCount + 1, 4] = entry[3];
workSheet.Cells[rowCount + 1, 5] = entry[4];
workSheet.Cells[rowCount + 1, 6] = entry[5];
workSheet.Cells[rowCount + 1, 7] = entry[6];
workSheet.Cells[rowCount + 1, 8] = entry[7];
workSheet.Cells[rowCount + 1, 9] = entry[8];
}