本文介绍使用spire.xls for .net来压缩excel工作表中的图片。
c#
using spire.xls;
namespace compressimgs
{
class program
{
static void main(string[] args)
{
//加载excel测试文档
workbook wb = new workbook();
wb.loadfromfile("test.xlsx");
//遍历工作表
foreach (worksheet sheet in wb.worksheets)
{
//遍历工作表中的所有图片
foreach (excelpicture picture in sheet.pictures)
{
picture.compress(30);//压缩图片
}
}
//保存文档
wb.savetofile("compressimgs.xlsx", excelversion.version2013);
system.diagnostics.process.start("compressimgs.xlsx");
}
}
}
vb.net
imports spire.xls
namespace compressimgs
class program
private shared sub main(args as string())
'加载excel测试文档
dim wb as new workbook()
wb.loadfromfile("test.xlsx")
'遍历工作表
for each sheet as worksheet in wb.worksheets
'遍历工作表中的所有图片
for each picture as excelpicture in sheet.pictures
'压缩图片
picture.compress(30)
next
next
'保存文档
wb.savetofile("compressimgs.xlsx", excelversion.version2013)
system.diagnostics.process.start("compressimgs.xlsx")
end sub
end class
end namespace
压缩结果: