Upload
Maximum file size: 1 MB. Files accepted: xls, xlsx, xlsb, ods.
Click here to browse files.fileerrors
Convert to
Source file:
filename
Target file type:
- Demo
- Java
- C# source
This demo shows you how to convert a Excel document (xls/xlsx/xlsb/ods) to PDF, HTML, Image.
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.core.spreadsheet.HTMLOptions;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertDemo {
public void convertDemo(String filePath, String convertTo, String resultFileName) throws IOException {
Workbook workbook = new Workbook();
workbook.loadFromFile(filePath);
ConvertFormat(workbook,convertTo,resultFileName);
}
private void ConvertFormat(Workbook workbook, String convertTo, String resultFileName) throws IOException {
switch (convertTo){
case "PDF":
workbook.getConverterSetting().setSheetFitToPage(true);
workbook.saveToFile(resultFileName + ".pdf", FileFormat.PDF);
break;
case "IMAGE":
BufferedImage[] images = (BufferedImage[]) new Image[workbook.getWorksheets().size()];
for (int i = 0; i < workbook.getWorksheets().size();i++){
images[i] = workbook.saveAsImage(i,300,300);
}
if (images != null && images.length > 0){
if (images.length == 1){
ImageIO.write(images[0],".PNG", new File(resultFileName+".png"));
}
}else {
for (int j = 0; j < images.length;j++){
String fileName = String.format("image-{0}.png",j);
ImageIO.write(images[j],".PNG",new File(fileName));
}
}
break;
case "HTML":
for (int i = 0; i < workbook.getWorksheets().size(); i++) {
HTMLOptions options = new HTMLOptions();
options.setImageEmbedded(true);
String htmlPath = String.format(resultFileName+"-{0}.html",i++);
workbook.getWorksheets().get(i).saveToHtml(htmlPath,options);
}
break;
case "TIFF":
workbook.saveToTiff(resultFileName+".tiff");
break;
case "XPS":
workbook.saveToFile(resultFileName+".xps",FileFormat.XPS);
break;
}
}
}
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the function you want, please request a free demo from us.
