在日常工作中可能會(huì)遇到將某文件轉(zhuǎn)換為其他格式的文件的情況,在本篇文章中將介紹如何用控件Free Spire.XLS for.NET(社區(qū)版)將Excel文檔轉(zhuǎn)為PDF、IMAGE、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等格式的文件,供各位參考。
工具使用:安裝之后,在項(xiàng)目中引用Spire.XLS.dll即可(該dll可在安裝文件下的Bin文件夾中獲?。?/p>
創(chuàng)新互聯(lián)公司"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機(jī)版的企業(yè)網(wǎng)站。實(shí)現(xiàn)跨屏營(yíng)銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動(dòng)網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營(yíng)銷需求!創(chuàng)新互聯(lián)公司具備承接各種類型的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作項(xiàng)目的能力。經(jīng)過(guò)10多年的努力的開(kāi)拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評(píng)。
下面將作詳細(xì)介紹:
(該組件也支持將Excel文檔里的圖表、形狀、SmartArt圖形以及圖像轉(zhuǎn)換為PDF)
//創(chuàng)建一個(gè)Wordbook類對(duì)象,并加載需要轉(zhuǎn)換的Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx",ExcelVersion.Version2010);
//將Excel文檔保存為PDF,并打開(kāi)轉(zhuǎn)換后的PDF文檔
workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF);
System.Diagnostics.Process.Start("result.pdf");
轉(zhuǎn)換結(jié)果:
//創(chuàng)建工作簿并加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//保存為PDF并打開(kāi)文檔
sheet.SaveToPdf("toPDF.pdf");
System.Diagnostics.Process.Start("toPDF.pdf");
轉(zhuǎn)換結(jié)果:
//初始化一個(gè)Workbook實(shí)例,并加載一個(gè)工作簿文件
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx");
//將第一張工作表保存為圖片
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToImage("sample.jpg");
//創(chuàng)建一個(gè)Workbook類實(shí)例,并從文件中加載一個(gè)工作表
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");
//獲取文檔中的第一個(gè)工作表
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);
//創(chuàng)建一個(gè)workbook類對(duì)象,并加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//保存為Html文件并打開(kāi)文檔
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);
//創(chuàng)建一個(gè)Wordkbook類對(duì)象并加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx");
//遍歷Excel工作簿,調(diào)用方法ToSVGStream()將文件保存到流,并通過(guò)流創(chuàng)建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類實(shí)例,并加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");
//獲取第一個(gè)工作表
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);
以上全部?jī)?nèi)容是本次關(guān)于Excel轉(zhuǎn)換為PDF、IMAGE、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM等格式文件的方法介紹,如果喜歡,歡迎轉(zhuǎn)載(轉(zhuǎn)載請(qǐng)注明出處)。
感謝瀏覽!