在日常工作中可能会遇到将某文件转换为其他格式的文件的情况,在本篇文章中将介绍如何在C#程序中将Excel文档转为PDF、IMAGE、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等格式的文件。
工具使用:Free Spire.XLS http://for.NET(免费版)
安装www.e-iceblue.cn安装后,在VS程序中添加引用Spire.XLS.dll即可(该dll可在安装文件下的Bin文件夹中获取)
下面将作详细介绍:
- Excel转PDF(该组件也支持将Excel文档里的图表、形状、SmartArt图形以及图像转换为PDF)
1.1 转整个Excel工作簿为PDF
//创建一个Wordbook类对象,并加载需要转换的Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:UsersAdministratorDesktoptest.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:UsersAdministratorDesktopSample.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:UsersAdministratorDesktopSample.xlsx");//将第一张工作表保存为图片
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToImage("sample.jpg");
2.2转指定范围的单元格为Image
//创建一个Workbook类实例,并从文件中加载一个工作表
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:UsersAdministratorDesktoptest.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:UsersAdministratorDesktopsample.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:UsersAdministratorDesktopsample1.xlsx");Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToFile("ExceltoTxt.txt", " ", Encoding.UTF8);
5. Excel转XML
Workbook wb = new Workbook();
wb.LoadFromFile(@"C:UsersAdministratorDesktopsample1.xlsx");
wb.SaveAsXml("result.xml");
6. Excel转XPS
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:UsersAdministratorDesktopsample.xlsx", ExcelVersion.Version2010);
workbook.SaveToFile("result.xps", Spire.Xls.FileFormat.XPS);
7. Excel转CSV
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:UsersAdministratorDesktoptest.xlsx");Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToFile("sample.csv", " ", Encoding.UTF8);
8. Excel转ODS
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:UsersAdministratorDesktoptest.xlsx");
workbook.SaveToFile("Result.ods", FileFormat.ODS);
9. Excel转SVG
//创建一个Wordkbook类对象并加载Excel文档Workbook workbook = new Workbook();workbook.LoadFromFile(@"C:UsersAdministratorDesktopSample.xlsx");//遍历Excel工作簿,调用方法ToSVGStream()将文件保存到流,并通过流创建SVG文件for (int i = 0; i < workbook.Worksheets.Count; i++){FileStream fs = new FileStream(string.Format("E:Program Filessheet-{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:UsersAdministratorDesktoptest.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:UsersAdministratorDesktoptest.xlsx", ExcelVersion.Version97to2003);
workbook.SaveToFile("result.xlsm", FileFormat.Version2007);
以上全部内容是本次关于Excel转换为PDF、IMAGE、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等格式文件的方法介绍.
End.