您好,登錄后才能下訂單哦!
在日常工作中可能會遇到將某文件轉換為其他格式的文件的情況,在本篇文章中將介紹如何用控件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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。