嘗試這個例子中這應該足以讓你開始 在頂部使用中添加該
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
你可能還需要增加在項目級的參考以及 確保當您選擇參考訂購列表 - >新增 - >和選擇基於安裝的版本正確版本的Microsoft Office互操作程序集的你有
版本大概12或14,我猜..
private static void Excel_FromDataTable(DataTable dt)
{
// Create an Excel object and add workbook...
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workbook = excel.Application.Workbooks.Add(true); // true for object template???
// Add column headings...
int iCol = 0;
foreach (DataColumn c in dt.Columns)
{
iCol++;
excel.Cells[1, iCol] = c.ColumnName;
}
// for each row of data...
int iRow = 0;
foreach (DataRow r in dt.Rows)
{
iRow++;
// add each row's cell data...
iCol = 0;
foreach (DataColumn c in dt.Columns)
{
iCol++;
excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
}
}
// Global missing reference for objects we are not defining...
object missing = System.Reflection.Missing.Value;
// If wanting to Save the workbook...
workbook.SaveAs("MyExcelWorkBook.xls",
Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
// If wanting to make Excel visible and activate the worksheet...
excel.Visible = true;
Excel.Worksheet worksheet = (Excel.Worksheet)excel.ActiveSheet;
((Excel._Worksheet)worksheet).Activate();
// If wanting excel to shutdown...
((Excel._Application)excel).Quit();
}
你自己試過什麼代碼..?還有什麼是DataTable的結構..?這是返回XML格式..?如果是這樣,請確認這種方式,我可以爲您提供一個關於如何迭代Web服務返回的XML數據的簡單解決方案.. – MethodMan 2012-02-16 15:01:11
@DJKRAZE:我不使用XML。查看我的文章中的更改(我添加了源代碼片段) – 2012-02-16 15:28:22
ok我會看看謝謝 – MethodMan 2012-02-16 15:31:32