Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等

时间:2022-03-20 03:52:11

标签:

在日常事情中可能会遇到将某文件转换为其他格局的文件的情况,在本篇文章中将介绍如何用控件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");

转换功效:

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");

转换功效:

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");

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);

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");

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);

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");