在日常工作中可能会遇到将某文件转换为其他格式的文件的情况,在本篇文章中将介绍如何用控件Free Spire.XLS for.NET(社区版)将Excel文档转为PDF、IMAGE、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等格式的文件,供各位参考。
工具使用:安装之后,在项目中引用Spire.XLS.dll即可(该dll可在安装文件下的Bin文件夹中获取)

下面将作详细介绍:

1. Excel转PDF

(该组件也支持将Excel文档里的图表、形状、SmartArt图形以及图像转换为PDF)

1.1 转整个Excel工作簿为PDF

//创建一个Wordbook类对象,并加载需要转换的Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx",ExcelVersion.Version2010);
//将Excel文档保存为PDF,并打开转换后的PDF文档
workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF);
System.Diagnostics.Process.Start("result.pdf");

转换结果:
C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

1.2 转指定工作表为PDF

//创建工作簿并加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.Worksheets[0];
//保存为PDF并打开文档
sheet.SaveToPdf("toPDF.pdf");
System.Diagnostics.Process.Start("toPDF.pdf");

转换结果:
C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

2. Excel转Image

2.1 将指定工作表转为Image

//初始化一个Workbook实例,并加载一个工作簿文件
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx");

//将第一张工作表保存为图片
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToImage("sample.jpg");

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

2.2 将工作表指定范围的单元格转为Image

//创建一个Workbook类实例,并从文件中加载一个工作表
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");

//获取文档中的第一个工作表
Worksheet sheet = workbook.Worksheets[0];

//指定单元格范围,保存为想要的图片格式
sheet.SaveToImage(3, 1, 4, 3).Save("image1.png", ImageFormat.Png);
sheet.SaveToImage(5, 1, 19,3).Save("image2.jpeg", ImageFormat.Jpeg);
sheet.SaveToImage(20, 1, 21, 3).Save("image3.bmp", ImageFormat.Bmp);
sheet.SaveToImage(22, 1, 23, 3).Save("image4.bmp", ImageFormat.Bmp);

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

3. Excel转HTML

//创建一个workbook类对象,并加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");

//获取第一个工作表
Worksheet sheet = workbook.Worksheets[0];
//保存为Html文件并打开文档
sheet.SaveToHtml("sample.html");
System.Diagnostics.Process.Start("sample.html");

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

4. Excel转 TXT

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample1.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToFile("ExceltoTxt.txt", " ", Encoding.UTF8);

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

5. Excel转XML

Workbook wb = new Workbook();
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\sample1.xlsx");
wb.SaveAsXml("result.xml");

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

6. Excel转XPS

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx", ExcelVersion.Version2010);
workbook.SaveToFile("result.xps", Spire.Xls.FileFormat.XPS);

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

7. Excel转CSV

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToFile("sample.csv", " ", Encoding.UTF8);

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

8. Excel转ODS

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");
workbook.SaveToFile("Result.ods", FileFormat.ODS);

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

9. Excel转SVG

            //创建一个Wordkbook类对象并加载Excel文档
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx");
//遍历Excel工作簿,调用方法ToSVGStream()将文件保存到流,并通过流创建SVG文件
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                FileStream fs = new FileStream(string.Format("E:\\Program Files\\sheet-{0}.svg", i), FileMode.Create);
                workbook.Worksheets[i].ToSVGStream(fs, 0, 0, 0, 0);
                fs.Flush();
                fs.Close();
         }

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

10. Excel转 EMF

//初始化Workbbok类实例,并加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.Worksheets[0];
//将指定大小范围的工作表保存为Emf格式
sheet.SaveToEMFImage("result.emf", 1, 1, 19, 6, System.Drawing.Imaging.EmfType.EmfPlusDual);

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

11. Excel转XLSM

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx", ExcelVersion.Version97to2003);
workbook.SaveToFile("result.xlsm", FileFormat.Version2007);

C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

以上全部内容是本次关于Excel转换为PDF、IMAGE、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等格式文件的方法介绍,如果喜欢,欢迎转载(转载请注明出处)。
感谢浏览!