在日常工作中可能会遇到将某文件转换为其他格式的文件的情况,在本篇文章中将介绍如何用控件Free Spire.XLS for.NET(社区版)将Excel文档转为PDF、IMAGE、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等格式的文件,供各位参考。
工具使用:安装之后,在项目中引用Spire.XLS.dll即可(该dll可在安装文件下的Bin文件夹中获取)
下面将作详细介绍:
(该组件也支持将Excel文档里的图表、形状、SmartArt图形以及图像转换为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文档
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");
转换结果:
//初始化一个Workbook实例,并加载一个工作簿文件
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx");
//将第一张工作表保存为图片
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToImage("sample.jpg");
//创建一个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);
//创建一个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");
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample1.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToFile("ExceltoTxt.txt", " ", Encoding.UTF8);
Workbook wb = new Workbook();
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\sample1.xlsx");
wb.SaveAsXml("result.xml");
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx", ExcelVersion.Version2010);
workbook.SaveToFile("result.xps", Spire.Xls.FileFormat.XPS);
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToFile("sample.csv", " ", Encoding.UTF8);
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");
workbook.SaveToFile("Result.ods", FileFormat.ODS);
//创建一个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();
}
//初始化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);
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx", ExcelVersion.Version97to2003);
workbook.SaveToFile("result.xlsm", FileFormat.Version2007);
以上全部内容是本次关于Excel转换为PDF、IMAGE、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等格式文件的方法介绍,如果喜欢,欢迎转载(转载请注明出处)。
感谢浏览!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。