110,499
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
string excelInputPath = "e:\\1.xlsx";
string imageOutputPath = "";
if (imageOutputPath.Trim().Length == 0)
{
imageOutputPath = Path.GetDirectoryName(excelInputPath)+Path.GetFileNameWithoutExtension(excelInputPath);
}
Workbook workbook = new Workbook();
workbook.Open(excelInputPath);
int i = 0;
ImageFormat imageFormat = ImageFormat.Jpeg;
foreach (Worksheet worksheet in workbook.Worksheets)
{
if (worksheet.Cells.Rows.Count > 0)
{
worksheet.SheetToImage(imageOutputPath + "_" + i.ToString()+"." + imageFormat,
ImageFormat.Jpeg);
i++;
}
}
nPages = 0;
if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
imageOutputPath = Path.GetDirectoryName(excelInputPath) + Path.GetFileNameWithoutExtension(excelInputPath);
//Open the template file
Workbook book = new Workbook(excelInputPath);
foreach (Worksheet sheet in book.Worksheets)
{
//Specify your print area if you want
//sheet.PageSetup.PrintArea = "A1:H8";
//To remove the white border around the image.
sheet.PageSetup.LeftMargin = 0;
sheet.PageSetup.RightMargin = 0;
sheet.PageSetup.BottomMargin = 0;
sheet.PageSetup.TopMargin = 0;
//Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
ImageFormat imageFormat = ImageFormat.Emf;
imgOptions.ImageFormat = imageFormat;
//Set only one page would be rendered for the image
imgOptions.OnePagePerSheet = true;
imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
//Create the SheetRender object based on the sheet with its
//ImageOrPrintOptions attributes
SheetRender sr = new SheetRender(sheet, imgOptions);
//Convert the image
sr.ToImage(nPages, imageOutputPath + "_" + nPages.ToString() + "." + imageFormat.ToString());
nPages++;
}
我做的excel的例子是3个worksheet,结果只有第0个worksheet被转换,按道理是应该转换出3张图片才对。
3张图片都不能使空的!!! 现在只有第0个不是空图片