excel 表格可能会因为不同设备、不同软件版本或字体缺失等问题,导致格式错乱或数据显示异常。转换为图片后,能确保数据的排版、格式和外观始终保持一致,无论在何种设备或平台上查看,都能呈现出固定的样式,避免了因环境差异而产生的显示问题。本文将介绍如何在.net 程序中通过c# 将excel转换为图片 (jpg、png等)。
通过c# 转换excel工作表到图片
免费spire.xls库提供的 savetoimage() 方法可将某个指定的工作表转换为jpg或png图片。步骤参考:
- 使用 workbook 类的 loadfromfile() 方法加载 excel 文档。
- 通过 workbook 类的 worksheets[] 属性获取文档中的指定工作表。
- 使用 worksheet 类的 savetoimage() 方法将工作表保存为指定格式的图片。
c#代码:
using spire.xls; namespace exceltoimage { class program { static void main(string[] args) { // 加载excel文档 workbook workbook = new workbook(); workbook.loadfromfile(@"e:\pythonexcel\计划.xlsx"); // 获取第一张工作表 worksheet sheet = workbook.worksheets[0]; // 将工作表保存为图片 sheet.savetoimage("excel转图片.jpg"); } } }
转换结果:
通过c# 转换指定单元格区域到图片
要实现该功能,可以先使用 worksheet.toimage(int firstrow, int firstcolumn, int lastrow, int lastcolumn) 方法一个指定的单元格区域转换为 image 对象,然后再使用 image.save() 方法将该对象保存为特定图片格式。
代码如下:
using spire.xls; using system.drawing.imaging; namespace exceltoimage { class program { static void main(string[] args) { // 加载excel文档 workbook workbook = new workbook(); workbook.loadfromfile(@"e:\pythonexcel\计划.xlsx"); // 获取第一张工作表 worksheet sheet = workbook.worksheets[0]; // 指定单元格区域并将其保存为特定图像格式 sheet.toimage(2, 2, 5, 6).save("单元格区域转图片.png", imageformat.png); } } }
知识扩展
c# 将 excel 转为 pdf
1.将整个 excel 工作薄转为 pdf
工作簿转为 pdf 的步骤如下:
- 创建 workbook 类的对象。
- 通过 workbook.loadfromfile(string filename) 方法加载 excel 文档。
- 通过 workbook.savetofile(string filename, fileformat fileformat) 方法保存为 pdf 到指定路径。
using spire.xls; namespace workbooktopdf { class program { static void main(string[] args) { //创建workbook类的实例 workbook workbook = new workbook(); //加载excel工作簿 workbook.loadfromfile("test.xlsx"); //将整个工作薄保存为pdf workbook.savetofile("workbooktopdf.pdf",fileformat.pdf); } } }
2.将指定 excel 工作表转为 pdf
工作表转为pdf的步骤如下:
- 创建 workbook 类的对象。
- 通过 workbook.loadfromfile(string filename) 方法加载 excel 文档。
- 通过 workbook.worksheets[] 属性获取指定工作表。
- 通过 worksheet.savetopdf(string filename, fileformat fielformat) 方法保存为 pdf 到指定路径。
using spire.xls; namespace worksheettopdf { class program { static void main(string[] args) { //创建workbook类的对象 workbook workbook = new workbook(); //加载excel文档 workbook.loadfromfile("test.xlsx"); //获取第2个工作表 worksheet sheet = workbook.worksheets[1]; //将工作表保存为pdf sheet.savetopdf("sheettopdf.pdf", fileformat.pdf); } } }
c#将 excel 转换为 html
1.将 excel 转换为 html
spire.xls for .net 支持使用 worksheet.savetohtml() 方法将特定的 excel 工作表转换为 html。下面列出了详细的步骤。
- 创建一个 wordbook 实例。
- 使用 workbook.loadfromfile() 方法加载 excel 示例文档。
- 使用 workbook.worksheets[] 属性获取特定工作表。
- 使用 worksheet.savetohtml() 方法将工作表保存为 html 文件。
using spire.xls; namespace xlstohtml { class program { static void main(string[] args) { //创建一个workbook实例 workbook workbook = new workbook(); //加载 excel 示例文档 workbook.loadfromfile(@"c: \users\administrator\desktop\sample.xlsx"); //获取文档的第一个工作表 worksheet sheet = workbook.worksheets[0]; //将工作表保存为 html sheet.savetohtml("exceltohtml.html"); } } }
2.将嵌入图像的 excel 转换为 html
以下是将 excel 工作表转换为嵌入图像的 html 的步骤。
- 创建一个 workbook 实例。
- 使用 workbook.loadfromfile() 方法加载 excel 示例文档。
- 使用 workbook.worksheets[] 属性获取特定工作表。
- 创建一个 htmloptions 实例。
- 将 imageembedded 设置为 true 以将图像嵌入 html。
- 使用 worksheet.savetohtml() 方法将工作表保存为 html 文件。
using spire.xls; using spire.xls.core.spreadsheet; namespace xlstohtml { class program { static void main(string[] args) { //创建一个workbook实例 workbook workbook = new workbook(); //加载 excel 示例文档 workbook.loadfromfile(@"c:\users\administrator\desktop\sample.xlsx"); //获取文档的第一个工作表 worksheet sheet = workbook.worksheets[0]; //创建一个 htmloptions 实例 htmloptions options = new htmloptions(); //将图像嵌入 html options.imageembedded = true; //将工作表保存为 html sheet.savetohtml("xls2html.html", options); } } }
到此这篇关于c#实现将excel表格转换为图片(jpg/ png)的文章就介绍到这了,更多相关c# excel转图片内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论