我有一個包含一些數據的列表數組。目前我可以在控制檯中看到輸出,現在試圖添加到excel文件中。任何人都可以解釋我該怎麼做。這裏是創建Excel表格並寫入內容的代碼。但是,如何結合這兩個代碼來查看excel中的輸出。我嘗試了幾種組合,但無法寫入excel。我是c#的新手。提前感謝!如何將列表數組寫入Excel文件
foreach (Match m in linkParser.Matches(html))
{
list.Add(m.Value);
Console.WriteLine(m.Value);
}
Excel.Application oApp; // to open excel
Excel.Worksheet oSheet;
Excel.Workbook oBook;
oApp = new Excel.Application();
oBook = oApp.Workbooks.Add();
oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
string fileTest = "output123.xlsx";
if (File.Exists(fileTest))
{
File.Delete(fileTest);
}
oSheet.Cells[1, 1] = "some value";
oBook.SaveAs(fileTest);
oBook.Close();
oApp.Quit();
有很多庫可以在[這個老問題]中做到這一點(http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp?rq=1 )。你也可以通過編寫HTML並將它稱爲.xls來破解一些東西,但它不是一個真正的Excel文件。 – Rup
一種可能性是自動化Office產品[使用主互操作程序集 - PIA](http://msdn.microsoft.com/en-us/library/dww0e04a%28v=vs.90%29.aspx)。使用PIA創建一個excel實例,通過它打開/創建一個工作簿,用數據填充工作表,保存並關閉excel實例。 – pasty