在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
我們開發的應用程式不斷與 Excel 試算表進行通信,以取得數據進行評估和獲得結果。 能夠以 C# 程式化生成 Excel 文件對我們的開發來說非常有幫助,這節省了我們的時間和精力。 在本教程中,我們將學習如何生成不同格式的 Excel 文件、設置單元格樣式以及使用高效的 C# 編程函數插入數據。
使用 IronXL Excel for C# 程式庫生成 Excel 文件,為您的項目提供一系列的函數用於生成和操作數據。 該程式庫可免費用於開發,當您準備好上線時可以購買許可證。如需跟隨本教程,您可以下載 IronXL 以生成或通过 Visual Studio 访问它并NuGet 畫廊.
Install-Package IronXL.Excel
在商業應用開發中,我們經常需要以程式方式生成各種類型的 Excel 文件。 為此,我們需要最簡單和最快捷的方法來生成各種類型的文件,並自動將它們保存到所需位置。
安裝IronXL後,我們可以使用功能來生成不同類型的 Excel 文件:
.xlsx
擴展名的 Excel 文件。.xls
擴展名的Excel文件。.csv
)檔案。.tsv
)檔案。.json
)檔案。可扩展标记语言(.xml
)檔案。
要生成任何類型的文件,我們首先需要創建一個 Excel。Workbook
.
//generate New WorkBook
WorkBook wb = WorkBook.Create();
//generate New WorkBook
WorkBook wb = WorkBook.Create();
'generate New WorkBook
Dim wb As WorkBook = WorkBook.Create()
上述代码行将创建一个新的 WorkBook
wb
,现在我们将创建一个工作表
物件。
//Generate New WorkSheet
WorkSheet ws = WorkBook.CreateWorkSheet("SheetName");
//Generate New WorkSheet
WorkSheet ws = WorkBook.CreateWorkSheet("SheetName");
'Generate New WorkSheet
Dim ws As WorkSheet = WorkBook.CreateWorkSheet("SheetName")
這將創建一個 WorkSheet
ws
,我們可以用它在 Excel 文件中插入數據。
首先,我們按照上述步驟生成WorkBook
和WorkSheet
。
然後,我們在其中插入資料以建立 .xlsx
副檔名的檔案。為此,IronXL 提供了一個單元格定址系統,使我們能夠以程式方式在特定單元格地址中插入資料。
//Insert data by cell addressing
WorkSheet ["CellAddress"].Value = "MyValue";
//Insert data by cell addressing
WorkSheet ["CellAddress"].Value = "MyValue";
'Insert data by cell addressing
WorkSheet ("CellAddress").Value = "MyValue"
它將在特定的單元格地址中插入一個我們稱之為 "MyValue" 的新值。 同樣,我們可以將資料插入到我們需要的多個儲存格中。之後,我們將把 Excel 文件保存在指定的路徑中,如下所示:
//specify file path and name
WorkBook.SaveAs("Path + FileName.xlsx");
//specify file path and name
WorkBook.SaveAs("Path + FileName.xlsx");
'specify file path and name
WorkBook.SaveAs("Path + FileName.xlsx")
這會在指定路徑中建立一個副檔名為.xlsx
的新 Excel 檔案。 在儲存時,別忘了在檔案名稱中寫上 .xlsx
副檔名。
更進一步探討如何在 C# 專案中創建 Excel 工作簿請查看這裡的程式碼範例。
/**
Generate XLSX File
anchor-generate-xlsx-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook of .xlsx Extension
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
//create workSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
ws ["C4"].Value = "IronXL";
//save the file as .xlsx
wb.SaveAs("sample.xlsx");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
/**
Generate XLSX File
anchor-generate-xlsx-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook of .xlsx Extension
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
//create workSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
ws ["C4"].Value = "IronXL";
//save the file as .xlsx
wb.SaveAs("sample.xlsx");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
'''
'''Generate XLSX File
'''anchor-generate-xlsx-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
'create new WorkBook of .xlsx Extension
Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
'create workSheet
Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
'insert data in the cells of WorkSheet
ws ("A1").Value = "Hello"
ws ("A2").Value = "World"
ws ("C4").Value = "IronXL"
'save the file as .xlsx
wb.SaveAs("sample.xlsx")
Console.WriteLine("successfully created.")
Console.ReadKey()
End Sub
您可以在此處查看新創建的 Excel 文件 sample.xlsx
的截圖:
修改後C4單元格的值的結果
也可以使用 IronXL 產生 .xls
文件。 為此,我們將使用`WorkBook.Create()函數如下:
WorkBook.Create(ExcelFileFormat.XLS)
WorkBook.Create(ExcelFileFormat.XLS)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WorkBook.Create(ExcelFileFormat.XLS)
這將創建一個以 .xls
擴展名的新 Excel 文件。 請記住,在為 Excel 文件命名時,必須在文件名後加上 .xls
擴展名,如下所示:
WorkBook.SaveAs("Path + FileName.xls");
WorkBook.SaveAs("Path + FileName.xls");
WorkBook.SaveAs("Path + FileName.xls")
現在,讓我們來看看如何生成具有 .xls
擴展名的 Excel 檔案的示例:
/**
Generate XLS File
anchor-generate-xls-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook of .xls Extension
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
//save the file as .xls
wb.SaveAs("sample.xls");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
/**
Generate XLS File
anchor-generate-xls-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook of .xls Extension
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
//save the file as .xls
wb.SaveAs("sample.xls");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
'''
'''Generate XLS File
'''anchor-generate-xls-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
'create new WorkBook of .xls Extension
Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS)
'create WorkSheet
Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
'insert data in the cells of WorkSheet
ws ("A1").Value = "Hello"
ws ("A2").Value = "World"
'save the file as .xls
wb.SaveAs("sample.xls")
Console.WriteLine("successfully created.")
Console.ReadKey()
End Sub
逗號分隔值(.csv
)文件在不同類型的組織中也扮演著保持數據的重要角色。 所以,我們還需要學習如何生成 .csv
文件並以程式方式插入數據。
我們可以使用與上述相同的過程,但在儲存時需要指定檔案名稱的 .csv
副檔名。 讓我們來看看在 C# 專案中如何建立 .csv
文件的範例:
/**
Generate CSV File
anchor-generate-csv-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook
WorkBook wb = WorkBook.Create();
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
//save the file as .csv
wb.SaveAsCsv("sample.csv");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
/**
Generate CSV File
anchor-generate-csv-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook
WorkBook wb = WorkBook.Create();
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
//save the file as .csv
wb.SaveAsCsv("sample.csv");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
'''
'''Generate CSV File
'''anchor-generate-csv-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
'create new WorkBook
Dim wb As WorkBook = WorkBook.Create()
'create WorkSheet
Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
'insert data in the cells of WorkSheet
ws ("A1").Value = "Hello"
ws ("A2").Value = "World"
'save the file as .csv
wb.SaveAsCsv("sample.csv")
Console.WriteLine("successfully created.")
Console.ReadKey()
End Sub
要能夠更多地與 CSV 文件互動,您可以按照本教程操作讀取 .csv
檔案.
有時我們需要生成制表符分隔值(.tsv
)以程式方式插入檔案和數據。
使用 IronXL,我們也可以生成 .tsv
擴展名文件,將數據插入其中,然後將其保存到所需位置。
讓我們看看如何生成 .tsv
擴展名文件的範例:
/**
Generate TSV File
anchor-generate-tsv-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook
WorkBook wb = WorkBook.Create();
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
//save the file as .tsv
wb.SaveAs("sample.tsv");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
/**
Generate TSV File
anchor-generate-tsv-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook
WorkBook wb = WorkBook.Create();
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
//save the file as .tsv
wb.SaveAs("sample.tsv");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
'''
'''Generate TSV File
'''anchor-generate-tsv-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
'create new WorkBook
Dim wb As WorkBook = WorkBook.Create()
'create WorkSheet
Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
'insert data in the cells of WorkSheet
ws ("A1").Value = "Hello"
ws ("A2").Value = "World"
'save the file as .tsv
wb.SaveAs("sample.tsv")
Console.WriteLine("successfully created.")
Console.ReadKey()
End Sub
我們可以自信地說,JavaScript Object Notation(.json
)文件是最常見的數據文件,並在幾乎所有軟體開發公司中使用。 因此,我們經常需要將資料儲存為 JSON 格式。 為此,我們需要最簡單的方法來生成 JSON 格式的文件並將數據插入其中。
在這種情況下,IronXL 是最佳選擇,我們可以輕鬆為 C# 生成這些文件。 讓我們看看這個例子。
/**
Generate JSON File
anchor-generate-json-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook
WorkBook wb = WorkBook.Create();
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "1";
ws ["A2"].Value = "john";
ws ["B1"].Value = "2";
ws ["B2"].Value = "alex";
ws ["C1"].Value = "3";
ws ["C2"].Value = "stokes";
//save the file as .json
wb.SaveAsJson("sample.json");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
/**
Generate JSON File
anchor-generate-json-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook
WorkBook wb = WorkBook.Create();
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "1";
ws ["A2"].Value = "john";
ws ["B1"].Value = "2";
ws ["B2"].Value = "alex";
ws ["C1"].Value = "3";
ws ["C2"].Value = "stokes";
//save the file as .json
wb.SaveAsJson("sample.json");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
'''
'''Generate JSON File
'''anchor-generate-json-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
'create new WorkBook
Dim wb As WorkBook = WorkBook.Create()
'create WorkSheet
Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
'insert data in the cells of WorkSheet
ws ("A1").Value = "1"
ws ("A2").Value = "john"
ws ("B1").Value = "2"
ws ("B2").Value = "alex"
ws ("C1").Value = "3"
ws ("C2").Value = "stokes"
'save the file as .json
wb.SaveAsJson("sample.json")
Console.WriteLine("successfully created.")
Console.ReadKey()
End Sub
並檢查新創建的 JSON 文件 sample.json
的截圖:
在 Visual Studio 中導航至 NuGet 套件管理員
在商業應用程式開發中,我們經常需要將資料儲存在可擴展標記語言中。(.xml
)文件格式。 這很重要,因為 .xml
文件數據對人和機器都可讀。
通過以下示例,我們將學習如何為 C# 生成 .xml
文件並以編程方式插入數據。
/**
Generate XML File
anchor-generate-xml-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook
WorkBook wb = WorkBook.Create();
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
//save the file as .json
wb.SaveAsXml("sample.xml");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
/**
Generate XML File
anchor-generate-xml-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
//create new WorkBook
WorkBook wb = WorkBook.Create();
//create WorkSheet
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
//insert data in the cells of WorkSheet
ws ["A1"].Value = "Hello";
ws ["A2"].Value = "World";
//save the file as .json
wb.SaveAsXml("sample.xml");
Console.WriteLine("successfully created.");
Console.ReadKey();
}
'''
'''Generate XML File
'''anchor-generate-xml-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
'create new WorkBook
Dim wb As WorkBook = WorkBook.Create()
'create WorkSheet
Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
'insert data in the cells of WorkSheet
ws ("A1").Value = "Hello"
ws ("A2").Value = "World"
'save the file as .json
wb.SaveAsXml("sample.xml")
Console.WriteLine("successfully created.")
Console.ReadKey()
End Sub
您可以閱讀更多有關轉換 Excel 試算表並以程式方式使用於 C# 專案的文件。
IronXL 庫還提供了多種功能來與 Excel 文件互動,例如儲存格數據格式化, 合併儲存格, 插入數學函數甚至管理圖表。