彩票走势图

Excel .NET组件Spire.XLS教程:在C#中将工作簿拆分为多个Excel文档

翻译|使用教程|编辑:李显亮|2020-12-17 09:58:11.930|阅读 299 次

概述: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最新版

*这么优秀的国产工具怎能错过呢!在线下单专享“一口价”,低至4579元起,立即购买!想要获取更多福利的朋友可以哦~


将工作簿拆分为多个Excel文档

本文将介绍如何通过将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

慧都是E-iceblue官方友好合作伙伴,如果您对spire.xls可以了解具体授权价格和使用机制。

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP