翻譯|使用教程|編輯:李顯亮|2020-12-17 09:58:11.930|閱讀 373 次
概述:Spire.XLS是一款專業的 .NET Excel 組件, 可以用在各種 .NET 框架中,無需依賴于 Microsoft Office Excel,使開發人員可以快速地在 .NET 平臺上完成對 Excel 的各種編程操作。本文將介紹如何在C#中將工作簿拆分為多個Excel文檔
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.XLS for .NET 是一款專業的 .NET Excel 組件, 它可以用在各種.NET 框架中,包括 .NET Core、ASP.NET 和 Windows Forms 等相關的.NET 應用程序。在Excel表格中圖片也是我們經常會使用到的工具,這時我們如何使用Spire.XLS來完成呢?這就是本系列教程將給大家詳細講解的。
本文將介紹如何通過將Spire.XLS與C#或VB.NET一起使用來拆分工作簿。
C#
using Spire.Xls; using System; namespace SplitWorkbook { class Program { static void Main(string[] args) { //Create a Workbook object Workbook wb = new Workbook(); //Load an Excel document wb.LoadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx"); //Declare a new Workbook variable Workbook newWb; //Declare a String variable String sheetName; //Specify the folder path, which is used to store the generated Excel files String folderPath = "C:\\Users\\Administrator\\Desktop\\Output\\"; //Loop through the worksheets in the source file for (int i = 0; i < wb.Worksheets.Count; i++) { //Initialize the Workbook object newWb = new Workbook(); //Remove the default sheets newWb.Worksheets.Clear(); //Add the the specific worksheet of the source document to the new workbook newWb.Worksheets.AddCopy(wb.Worksheets[i]); //Get the worksheet name sheetName = wb.Worksheets[i].Name; //Save the new workbook to the specified folder newWb.SaveToFile(folderPath + sheetName + ".xlsx", ExcelVersion.Version2013); } } } }
VB.NET
Imports Spire.Xls Imports System Namespace SplitWorkbook Class Program Shared Sub Main(ByVal args() As String) 'Create a Workbook object Dim wb As Workbook = New Workbook() 'Load an Excel document wb.LoadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx") 'Declare a new Workbook variable Dim NewWb As Workbook 'Declare a String variable Dim sheetName As String 'Specify the folder path, which is used to store the generated Excel files Dim folderPath As String = "C:\\Users\\Administrator\\Desktop\\Output\\" 'Loop through the worksheets in the source file Dim i As Integer For i = 0 To wb.Worksheets.Count- 1 Step i + 1 'Initialize the Workbook object NewWb = New Workbook() 'Remove the default sheets NewWb.Worksheets.Clear() 'Add the the specific worksheet of the source document to the new workbook NewWb.Worksheets.AddCopy(wb.Worksheets(i)) 'Get the worksheet name sheetName = wb.Worksheets(i).Name 'Save the new workbook to the specified folder NewWb.SaveToFile(folderPath + sheetName + ".xlsx", ExcelVersion.Version2013) Next End Sub End Class End Namespace
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn