[Java] 利用POI读取excel文件,转换为html →→→→→进入此内容的聊天室

来自 , 2019-12-07, 写在 Java, 查看 116 次.
URL http://www.code666.cn/view/cf67355a
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import org.apache.poi.hssf.usermodel.HSSFCell;
  7. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  8. import org.apache.poi.hssf.usermodel.HSSFFont;
  9. import org.apache.poi.hssf.usermodel.HSSFPalette;
  10. import org.apache.poi.hssf.usermodel.HSSFRow;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.hssf.util.HSSFColor;
  13. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  14. import org.apache.poi.ss.usermodel.Sheet;
  15. import org.apache.poi.ss.util.CellRangeAddress;
  16.  
  17. /**
  18.  * 利用POI读取excel,转换为html
  19.  *
  20.  */
  21. public class POIReadExcel {
  22.         public static void main(String[] args) {
  23.                 try {
  24.                         POIReadExcel poire = new POIReadExcel();
  25.                         String path = "D:\\test.xls";
  26.                         File sourcefile = new File(path);
  27.                         InputStream is = new FileInputStream(sourcefile);
  28.                         POIFSFileSystem fs = new POIFSFileSystem(is);
  29.                         HSSFWorkbook wb = new HSSFWorkbook(fs);
  30.                         System.out.println(poire.getExcelInfo(wb));
  31.                         is.close();
  32.                 } catch (Exception e) {
  33.                         e.printStackTrace();
  34.                 }
  35.         }
  36.  
  37.         public String getExcelInfo(HSSFWorkbook wb) throws Exception {
  38.                 StringBuffer sb = new StringBuffer();
  39.                 Sheet sheet = wb.getSheetAt(0);
  40.                 int lastRowNum = sheet.getLastRowNum();
  41.                 Map map[] = getRowSpanColSpanMap(sheet);
  42.                 sb.append(" ");
  43.                 HSSFRow row = null;
  44.                 HSSFCell cell = null;
  45.                 // System.out.println(sheet.getPhysicalNumberOfRows());
  46.                 for (int rowNum = sheet.getFirstRowNum(); rowNum < lastRowNum; rowNum++) {
  47.                         row = (HSSFRow) sheet.getRow(rowNum);
  48.                         if (row == null) {
  49.                                 sb.append(" ");
  50.                                 continue;
  51.                         }
  52.                         sb.append(" ");
  53.                         int lastColNum = row.getLastCellNum();
  54.                         for (int colNum = 0; colNum < lastColNum; colNum++) {
  55.                                 cell = row.getCell(colNum);
  56.                                 if (cell == null) {
  57.                                         sb.append(" ");
  58.                                         continue;
  59.                                 }
  60.                                 String stringValue = getCellValue(cell);
  61.                                 if (map[0].containsKey(rowNum + "," + colNum)) {
  62.                                         String pointString = map[0].get(rowNum + "," + colNum);
  63.                                         map[0].remove(rowNum + "," + colNum);
  64.                                         int bottomeRow = Integer.valueOf(pointString.split(",")[0]);
  65.                                         int bottomeCol = Integer.valueOf(pointString.split(",")[1]);
  66.                                         int rowSpan = bottomeRow - rowNum + 1;
  67.                                         int colSpan = bottomeCol - colNum + 1;
  68.                                         sb.append(" ");
  69.                                 } else if (map[1].containsKey(rowNum + "," + colNum)) {
  70.                                         map[1].remove(rowNum + "," + colNum);
  71.                                         continue;
  72.                                 } else {
  73.                                         sb.append(" ");
  74.                                 }
  75.                                 HSSFCellStyle cellStyle = cell.getCellStyle();
  76.                                 if (cellStyle != null) {
  77.                                         short alignment = cellStyle.getAlignment();
  78.                                         sb.append("align='" + convertAlignToHtml(alignment) + "' ");
  79.                                         short verticalAlignment = cellStyle.getVerticalAlignment();
  80.                                         sb.append("valign='"
  81.                                                         + convertVerticalAlignToHtml(verticalAlignment)
  82.                                                         + "' ");
  83.                                         HSSFFont hf = cellStyle.getFont(wb);
  84.                                         short boldWeight = hf.getBoldweight();
  85.                                         short fontColor = hf.getColor();
  86.                                         sb.append("style='");
  87.                                         HSSFPalette palette = wb.getCustomPalette(); // 类HSSFPalette用于求的颜色的国际标准形式
  88.                                         HSSFColor hc = palette.getColor(fontColor);
  89.                                         sb.append("font-weight:" + boldWeight + ";"); // 字体加粗
  90.                                         // System.out.println(hf.getFontHeight());
  91.                                         sb.append("font-size: " + hf.getFontHeight() / 2 + "%;"); // 字体大小
  92.                                         String fontColorStr = convertToStardColor(hc);
  93.                                         if (fontColorStr != null && !"".equals(fontColorStr.trim())) {
  94.                                                 sb.append("color:" + fontColorStr + ";"); // 字体颜色
  95.                                         }
  96.                                         short bgColor = cellStyle.getFillForegroundColor();
  97.                                         hc = palette.getColor(bgColor);
  98.                                         String bgColorStr = convertToStardColor(hc);
  99.                                         if (bgColorStr != null && !"".equals(bgColorStr.trim())) {
  100.                                                 sb.append("background-color:" + bgColorStr + ";"); // 背景颜色
  101.                                         }
  102.                                         short borderColor = cellStyle.getBottomBorderColor();
  103.                                         hc = palette.getColor(borderColor);
  104.                                         String borderColorStr = convertToStardColor(hc);
  105.                                         if (borderColorStr != null
  106.                                                         && !"".equals(borderColorStr.trim())) {
  107.                                                 sb.append("border-color:" + borderColorStr + ";"); // 边框颜色
  108.                                         }
  109.                                         // boolean borderBoolean = cellStyle.getWrapText();
  110.                                         //
  111.                                         // if(borderBoolean){
  112.                                         // sb.append("border-style: inset;");
  113.                                         // }
  114.                                         sb.append("' ");
  115.                                 }
  116.                                 sb.append(">");
  117.                                 if (stringValue == null || "".equals(stringValue.trim())) {
  118.                                         sb.append(" ");
  119.                                 } else {
  120.                                         // 将ascii码为160的空格转换为html下的空格(&nbsp;)
  121.                                         sb.append(stringValue.replace(String.valueOf((char) 160),
  122.                                                         "&nbsp;"));
  123.                                 }
  124.                                 sb.append(" ");
  125.                         }
  126.                         sb.append(" ");
  127.                 }
  128.                 sb.append(" ");
  129.                 return sb.toString();
  130.         }
  131.  
  132.         @SuppressWarnings("unchecked")
  133.         private Map[] getRowSpanColSpanMap(Sheet sheet) {
  134.                 Map map0 = new HashMap();
  135.                 Map map1 = new HashMap();
  136.                 int mergedNum = sheet.getNumMergedRegions();
  137.                 CellRangeAddress range = null;
  138.                 for (int i = 0; i < mergedNum; i++) {
  139.                         range = sheet.getMergedRegion(i);
  140.                         int topRow = range.getFirstRow();
  141.                         int topCol = range.getFirstColumn();
  142.                         int bottomRow = range.getLastRow();
  143.                         int bottomCol = range.getLastColumn();
  144.                         map0.put(topRow + "," + topCol, bottomRow + "," + bottomCol);
  145.                         // System.out.println(topRow + "," + topCol + "," + bottomRow + ","
  146.                         // + bottomCol);
  147.                         int tempRow = topRow;
  148.                         while (tempRow <= bottomRow) {
  149.                                 int tempCol = topCol;
  150.                                 while (tempCol <= bottomCol) {
  151.                                         map1.put(tempRow + "," + tempCol, "");
  152.                                         tempCol++;
  153.                                 }
  154.                                 tempRow++;
  155.                         }
  156.                         map1.remove(topRow + "," + topCol);
  157.                 }
  158.                 Map[] map = { map0, map1 };
  159.                 return map;
  160.         }
  161.  
  162.         private String convertAlignToHtml(short alignment) {
  163.                 String align = "left";
  164.                 switch (alignment) {
  165.                 case HSSFCellStyle.ALIGN_LEFT:
  166.                         align = "left";
  167.                         break;
  168.                 case HSSFCellStyle.ALIGN_CENTER:
  169.                         align = "center";
  170.                         break;
  171.                 case HSSFCellStyle.ALIGN_RIGHT:
  172.                         align = "right";
  173.                         break;
  174.                 default:
  175.                         break;
  176.                 }
  177.                 return align;
  178.         }
  179.  
  180.         private String convertVerticalAlignToHtml(short verticalAlignment) {
  181.                 String valign = "middle";
  182.                 switch (verticalAlignment) {
  183.                 case HSSFCellStyle.VERTICAL_BOTTOM:
  184.                         valign = "bottom";
  185.                         break;
  186.                 case HSSFCellStyle.VERTICAL_CENTER:
  187.                         valign = "center";
  188.                         break;
  189.                 case HSSFCellStyle.VERTICAL_TOP:
  190.                         valign = "top";
  191.                         break;
  192.                 default:
  193.                         break;
  194.                 }
  195.                 return valign;
  196.         }
  197.  
  198.         private String convertToStardColor(HSSFColor hc) {
  199.                 StringBuffer sb = new StringBuffer("");
  200.                 if (hc != null) {
  201.                         if (HSSFColor.AUTOMATIC.index == hc.getIndex()) {
  202.                                 return null;
  203.                         }
  204.                         sb.append("#");
  205.                         for (int i = 0; i < hc.getTriplet().length; i++) {
  206.                                 sb.append(fillWithZero(Integer.toHexString(hc.getTriplet()[i])));
  207.                         }
  208.                 }
  209.                 return sb.toString();
  210.         }
  211.  
  212.         private String fillWithZero(String str) {
  213.                 if (str != null && str.length() < 2) {
  214.                         return "0" + str;
  215.                 }
  216.                 return str;
  217.         }
  218.  
  219.         private String getCellValue(HSSFCell cell) {
  220.                 switch (cell.getCellType()) {
  221.                 case HSSFCell.CELL_TYPE_NUMERIC:
  222.                         DecimalFormat format = new DecimalFormat("#0.##");
  223.                         return format.format(cell.getNumericCellValue());
  224.                         // return String.valueOf(cell.getNumericCellValue());
  225.                 case HSSFCell.CELL_TYPE_STRING:
  226.                         return cell.getStringCellValue();
  227.                         // case HSSFCell.CELL_TYPE_FORMULA:
  228.                         //
  229.                         // return cell.getCellFormula();
  230.                 default:
  231.                         return "";
  232.                 }
  233.         }
  234. }
  235.  

回复 "利用POI读取excel文件,转换为html"

这儿你可以回复上面这条便签

captcha