C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等
2021-04-21 11:26
标签:C# .NET EXCEL API EXCEL文档转换 免费类库 控件 下面将作详细介绍: (该组件也支持将Excel文档里的图表、形状、SmartArt图形以及图像转换为PDF) 转换结果: 转换结果: 以上全部内容是本次关于Excel转换为PDF、IMAGE、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等格式文件的方法介绍,如果喜欢,欢迎转载(转载请注明出处)。 C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等 标签:C# .NET EXCEL API EXCEL文档转换 免费类库 控件 原文地址:http://blog.51cto.com/eiceblue/2087681
工具使用:安装之后,在项目中引用Spire.XLS.dll即可(该dll可在安装文件下的Bin文件夹中获取)
1. Excel转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");
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");
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
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);
感谢浏览!
上一篇:windows服务开发
下一篇:【知识点】VS调试问题(C#)
文章标题:C# 文档转换—Excel 转PDF/IMAGE/HTML/TXT/XML/XPS/CSV/ODS等
文章链接:http://soscw.com/index.php/essay/77576.html