簡體   English   中英

OpenXML SDK-Excel范圍

[英]OpenXML SDK - Excel Ranges

使用Microsoft的OpenXML SDK,是否可以使用范圍將數據集插入Excel?

以前,Excel API(互操作程序集)可讓您執行此操作。 現在,至少通過嘗試並閱讀示例,我發現的唯一方法是使用循環創建每一行和單元格。

我已經看到了一些3rd Party工具可以做到這一點,但我想開箱即用。 謝謝。

忘了使用互操作,您將不得不在本地安裝excel。 我曾經與excel一起使用過的最好的第三方工具是Gembox Spreedsheet,它是免費的:

http://www.gemboxsoftware.com/

網站上的“支持”選項卡中有關於如何使用該軟件的完整文章。 這是一個從數據集中插入數據到excel的容易程度的示例:

// Create new ExcelFile.
ExcelFile ef2 = new ExcelFile();

// Imports all the tables from DataSet to new file.
foreach (DataTable dataTable in dataSet.Tables)
{
    // Add new worksheet to the file.
    ExcelWorksheet ws = ef2.Worksheets.Add(dataTable.TableName);

    // Change the value of the first cell in the DataTable.
    dataTable.Rows[0][0] = "This is new file!";

    // Insert the data from DataTable to the worksheet starting at cell "A1".
    ws.InsertDataTable(dataTable, "A1", true);
}

// Save the file to XLS format.
ef2.SaveXls("DataSet.xls");

您可以像這樣使用openXML讀寫文件:

ExcelFile ef = new ExcelFile();

// Loads OpenXML file.
ef.LoadXlsx("filename.xlsx", XlsxOptions.None);

// Selects first worksheet.
ExcelWorksheet ws = ef.Worksheets[0];

// Change the value of the cell "A1".
ws.Cells["A1"].Value = "Hello world!";

// Saves the file in OpenXML format.
ef.SaveXlsx("NewFile.xlsx")

我已經多次使用此.Net組件,並且取得了巨大的成功,並且代碼量很少。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM