C# Excel转PDF、image、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM
程序员文章站
2022-03-29 13:13:13
...
文档转换在工作可能会比较常用到,在本篇博客中将介绍用免费类库Free Spire.XLS for .NET来转换Excel文档为多种格式的文档,如PDF、Image、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等
本文转自博客:http://blog.51cto.com/eiceblue/2087681
类库获取地址:Free Spire.XLS for .NET(dll文件在安装路径下的Bin文件夹中获取)
1.Excel转PDF
这里转整个Excel文档为PDF,也可以转指定工作表为PDF
//创建一个Wordbook类对象,并加载需要转换的Excel文档 Workbook workbook = new Workbook(); workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx",ExcelVersion.Version2010); //获取第一个工作表 //Worksheet sheet = workbook.Worksheets[0]; //将Excel文档保存为PDF,并打开转换后的PDF文档 workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF); System.Diagnostics.Process.Start("result.pdf");
测试效果
1.转整个文档
2.转指定工作表
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");
测试效果:
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);
测试效果:
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");
测试效果:
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);
测试效果:
5. Excel转xml
Workbook wb = new Workbook(); wb.LoadFromFile(@"C:\Users\Administrator\Desktop\sample1.xlsx"); wb.SaveAsXml("result.xml");
测试效果:
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);
测试效果:
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);
测试效果:
8. Excel转ods
Workbook workbook = new Workbook(); workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx"); workbook.SaveToFile("Result.ods", FileFormat.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(); }
测试效果:
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);
测试效果:
11. Excel转xlsm
Workbook workbook = new Workbook(); workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx", ExcelVersion.Version97to2003); workbook.SaveToFile("result.xlsm", FileFormat.Version2007);
测试效果: