java项目中Excel文件预览

时间:2024-03-08 13:41:24
  1 package com.linkonworks.df.busi.utils;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.StringWriter;
  8 
  9 import javax.xml.parsers.DocumentBuilderFactory;
 10 import javax.xml.parsers.ParserConfigurationException;
 11 import javax.xml.transform.OutputKeys;
 12 import javax.xml.transform.Transformer;
 13 import javax.xml.transform.TransformerException;
 14 import javax.xml.transform.TransformerFactory;
 15 import javax.xml.transform.dom.DOMSource;
 16 import javax.xml.transform.stream.StreamResult;
 17 
 18 import org.apache.poi.hssf.converter.ExcelToHtmlConverter;
 19 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 20 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 21 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 22 
 23 
 24 /**
 25  * <p>
 26  * @ClassName: FilePreview
 27  * </p>
 28  * <p>
 29  * Description: Excel文件转为html元素
 30  * </p>
 31  * 
 32  * @author 周宣
 33  * @date 2015年12月21日
 34  */
 35 public class FilePreview {
 36     /**
 37      * <p>
 38      * Title: convertExceltoHtml
 39      * </p>
 40      * <p>
 41      * Description: 将2003版Excel文件转为html标签元素
 42      * </p>
 43      * 
 44      * @author 周宣
 45      * @param excelFile
 46      * @return
 47      * @throws IOException
 48      * @throws ParserConfigurationException
 49      * @throws TransformerException
 50      * @throws InvalidFormatException
 51      */
 52     public static String convertExceltoHtml(String path) throws IOException,ParserConfigurationException, TransformerException,InvalidFormatException {
 53         HSSFWorkbook workBook = null;
 54         String content = null;
 55         StringWriter writer = null;
 56         File excelFile = new File(path);
 57         InputStream is = new FileInputStream(excelFile);;
 58         //判断Excel文件是2003版还是2007版
 59         String suffix = path.substring(path.lastIndexOf("."));
 60         if(suffix.equals(".xlsx")){
 61             //将07版转化为03版
 62             Xssf2Hssf xlsx2xls = new Xssf2Hssf();
 63             XSSFWorkbook xSSFWorkbook = new XSSFWorkbook(is);
 64             workBook = new HSSFWorkbook();
 65             xlsx2xls.transformXSSF(xSSFWorkbook, workBook);
 66             
 67         }else{
 68             workBook = new HSSFWorkbook(is);
 69         }
 70         try {
 71             ExcelToHtmlConverter converter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
 72             converter.setOutputColumnHeaders(false);// 不显示列的表头
 73             converter.setOutputRowNumbers(false);// 不显示行的表头
 74             converter.processWorkbook(workBook);
 75 
 76             writer = new StringWriter();
 77             Transformer serializer = TransformerFactory.newInstance().newTransformer();
 78             serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
 79             serializer.setOutputProperty(OutputKeys.INDENT, "yes");
 80             serializer.setOutputProperty(OutputKeys.METHOD, "html");
 81             serializer.transform(new DOMSource(converter.getDocument()),
 82                     new StreamResult(writer));
 83 
 84             content = writer.toString();
 85             writer.close();
 86         } finally {
 87             try {
 88                 if (is != null) {
 89                     is.close();
 90                 }
 91                 if (writer != null) {
 92                     writer.close();
 93                 }
 94             } catch (IOException e) {
 95                 e.printStackTrace();
 96             }
 97         }
 98         return content;
 99     }
100 
101 
102 
103 }