• JAVA-读取excel转成html 将excel表格转换为HTML文件格式 转成前端表格样式



    博客背景:JAVA项目,将Excel转换为HTML的便捷工具类。可快速上手使用~

    1、调用函数

    excelPositon为存放excel的位置;htmlPositon为将要存放html的位置。
    可以直接设置位置为磁盘中指定位置;如下默认为项目运行的上一级目录

    	public void excel2Html() {
            String excelPositon = "../test.xlsx";
            String htmlPositon =  "../test.html";
            //excel转html;src为excel位置,htmlPositon为存放html文件位置
            Excel2HtmlUtil.readExcelToHtml(excelPositon, htmlPositon, true, "xlsx", "test");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、excel转html工具类

    创建相应工具类Excel2HtmlUtil
    改编了一些表头、第一列的样式;还有链接时的样式。如无需可以在对应位置去掉即可~
    调用函数readExcelToHtml()可直接使用转换~

    //excel转html工具类
    package com.xx.util;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    import org.apache.poi.xssf.usermodel.XSSFColor;
    import org.apache.poi.xssf.usermodel.XSSFFont;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.*;
    import java.text.DecimalFormat;
    import java.text.SimpleDateFormat;
    import java.util.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class Excel2HtmlUtil {
        /**
         *
         * @param filePath    excel源文件文件的路径
         * @param htmlPositon 生成的html文件的路径
         * @param isWithStyle 是否需要表格样式 包含 字体 颜色 边框 对齐方式
         * @throws Exception
         *
         */
        public static String readExcelToHtml(String filePath, String htmlPositon, boolean isWithStyle,String type,String attname) throws Exception {
            InputStream is = null;
            String htmlExcel = null;
            Map<String,String> stylemap = new HashMap<String,String>();
            try {
                if("csv".equalsIgnoreCase(type)) {
                    htmlExcel = getCSVInfo(filePath,htmlPositon);
                    writeFile1(htmlExcel, htmlPositon,stylemap,attname);
                }else {
                    File sourcefile = new File(filePath);
                    is = new FileInputStream(sourcefile);
                    Workbook wb = WorkbookFactory.create(is);
                    if (wb instanceof XSSFWorkbook) { // 03版excel处理方法
                        XSSFWorkbook xWb = (XSSFWorkbook) wb;
                        htmlExcel = getExcelInfo(xWb, isWithStyle,stylemap);
                    } else if (wb instanceof HSSFWorkbook) { // 07及10版以后的excel处理方法
                        HSSFWorkbook hWb = (HSSFWorkbook) wb;
                        htmlExcel = getExcelInfo(hWb, isWithStyle,stylemap);
                    }
                    writeFile(htmlExcel, htmlPositon,stylemap,attname);
                }
            } catch (Exception e) {
                System.out.println("文件被损坏或不能打开,无法预览");
                //throw new Exception("文件被损坏或不能打开,无法预览");
            } finally {
                try {
                    if(is!=null){
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return htmlPositon;
        }
    
        private static void getcscvvalue(BufferedReader reader,List col,String oldvalue,List list) {
            String line = null;
            try {
                while((line=reader.readLine())!=null){
                    String[] item = line.split(",",-1);
                    boolean isbreak = false;
                    for(int i=0;i<item.length;i++) {
                        String value = item[i];
                        if(value.endsWith("\"")) {
                            value = oldvalue+value;
                            col.add(value);
                        }else if(item.length==1) {
                            value = oldvalue+value;
                            getcscvvalue(reader,col,value,list);
                            isbreak = true;
                        }else if(value.startsWith("\"")){
                            getcscvvalue(reader,col,value,list);
                            isbreak = true;
                        }else {
                            col.add(value);
                        }
                    }
                    if(!isbreak) {
                        list.add(col);
                        col = new ArrayList();
                    }
                }
            } catch (IOException e) {
            }
        }
    
        private static String getCSVInfo(String filePath,String htmlPositon) {
            StringBuffer sb = new StringBuffer();
            DataInputStream in = null;
            try {
                in=new DataInputStream(new FileInputStream(filePath));
                BufferedReader reader=new BufferedReader(new InputStreamReader(in));
                //reader.readLine();
                String line = null;
                List list = new ArrayList();
                while((line=reader.readLine())!=null){
                    String[] item = line.split(",");
                    List col = new ArrayList();
                    for(int i=0;i<item.length;i++) {
                        String value = item[i];
                        if(value.startsWith("\"")) {
                            getcscvvalue(reader,col,value,list);
                        }else {
                            col.add(value);
                        }
                    }
                    list.add(col);
                }
                sb.append("");for(int i=0;i<list.size();i++){List col =(List) list.get(i);if(col==null||col.size()==0){
                        sb.append("");}
                    sb.append("");for(int j=0;j<col.size();j++){String value =(String) col.get(j);if(value ==null||"".equals(value)){
                            sb.append("");continue;}else{
                            sb.append("");}}
                    sb.append("");}
                sb.append("
    "+value+"
    "
    ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } //读取excel文件,返回转换后的html字符串 private static String getExcelInfo(Workbook wb, boolean isWithStyle, Map<String,String> stylemap) { StringBuffer sb = new StringBuffer(); StringBuffer ulsb = new StringBuffer(); ulsb.append("
      "); int num = wb.getNumberOfSheets(); //遍历excel文件里的每一个sheet for(int i=0;i<num;i++) { Sheet sheet = wb.getSheetAt(i);// 获取第i个Sheet的内容 String sheetName = sheet.getSheetName(); if(i==0) { //ulsb.append("
    • "+sheetName+"
    • ");
      ulsb.append("

      " + sheetName + "

      "
      ); }else { //sheet名字 加上了蓝色底色 //ulsb.append("
    • "+sheetName+"
    • ");
      ulsb.append("

      " + sheetName + "

      "
      ); } int lastRowNum = sheet.getLastRowNum(); Map<String, String> map[] = getRowSpanColSpanMap(sheet); Map<String, String> map1[] = getRowSpanColSpanMap(sheet); sb.append(");if(i==0){ sb.append("class='block'");} sb.append(">");Row row =null;// 兼容Cell cell =null;// 兼容int maxRowNum =0;int maxColNum =0;//遍历每一行for(int rowNum = sheet.getFirstRowNum(); rowNum <= lastRowNum; rowNum++){ row = sheet.getRow(rowNum);if(row ==null){continue;}int lastColNum = row.getLastCellNum();for(int colNum =0; colNum < lastColNum; colNum++){ cell = row.getCell(colNum);if(cell ==null){// 特殊情况 空白的单元格会返回nullcontinue;}String stringValue =getCellValue1(cell);if(map1[0].containsKey(rowNum +","+ colNum)){ map1[0].remove(rowNum +","+ colNum);if(maxRowNum<rowNum){ maxRowNum = rowNum;}if(maxColNum<colNum){ maxColNum = colNum;}}elseif(map1[1].containsKey(rowNum +","+ colNum)){ map1[1].remove(rowNum +","+ colNum);if(maxRowNum<rowNum){ maxRowNum = rowNum;}if(maxColNum<colNum){ maxColNum = colNum;}continue;}if(stringValue ==null||"".equals(stringValue.trim())){continue;}else{if(maxRowNum<rowNum){ maxRowNum = rowNum;}if(maxColNum<colNum){ maxColNum = colNum;}}}}for(int rowNum = sheet.getFirstRowNum(); rowNum <= maxRowNum; rowNum++){ row = sheet.getRow(rowNum);if(row ==null){ sb.append("");continue;} sb.append("");int lastColNum = row.getLastCellNum();for(int colNum =0; colNum <= maxColNum; colNum++){ cell = row.getCell(colNum);if(cell ==null){// 特殊情况 空白的单元格会返回null sb.append("");continue;}String stringValue =getCellValue(cell);if(stringValue.equals("")){// 空字符串返空 sb.append(stringValue.replace(String.valueOf((char)160)," "));}String pattrenStr ="[a-zA-z]+://[^\\s]*";Pattern pattern =Pattern.compile(pattrenStr);Matcher matcher = pattern.matcher(stringValue);if(map[0].containsKey(rowNum +","+ colNum)){String pointString = map[0].get(rowNum +","+ colNum); map[0].remove(rowNum +","+ colNum);int bottomeRow =Integer.valueOf(pointString.split(",")[0]);int bottomeCol =Integer.valueOf(pointString.split(",")[1]);int rowSpan = bottomeRow - rowNum +1;int colSpan = bottomeCol - colNum +1;if(rowNum ==0){ sb.append("");} sb.append("");} sb.append("
      + rowSpan + "' colspan= '" + colSpan + "' "); }else{ sb.append("); } } else if (map[1].containsKey(rowNum + "," + colNum)) { map[1].remove(rowNum + "," + colNum); continue; } else { if(rowNum == 0 || colNum == 0){ //列表头加粗&底色 sb.append("); }else{ sb.append("); } } if (isWithStyle) { dealExcelStyle(wb, sheet, cell, sb,stylemap);// 处理单元格样式 } sb.append(">"); //如果单元格为空要判断该单元格是不是通过其他单元格计算得到的 if (stringValue == null) { // if (stringValue == null || "".equals(stringValue.trim())) { FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); if (evaluator.evaluate(cell) != null) { //如果单元格的值是通过其他单元格计算来的,则通过单元格计算获取 String cellnumber = evaluator.evaluate(cell).getNumberValue() + ""; //如果单元格的值是小数,保留两位 if (null != cellnumber && cellnumber.contains(".")) { String[] decimal = cellnumber.split("\\."); if (decimal[1].length() > 2) { int num1 = decimal[1].charAt(0) - '0'; int num2 = decimal[1].charAt(1) - '0'; int num3 = decimal[1].charAt(2) - '0'; if (num3 == 9) { num2 = 0; } else if (num3 >= 5) { num2 = num2 + 1; } cellnumber = decimal[0] + "." + num1 + num2; } } stringValue = cellnumber; } sb.append(stringValue.replace(String.valueOf((char) 160), " ")); } else { //如果是链接则使用该语句 if(matcher.matches()){ sb.append("+ stringValue + "\">点击查看内容"); }else{ // 将ascii码为160的空格转换为html下的空格( ) sb.append(stringValue.replace(String.valueOf((char) 160), " ")); } } sb.append("
      "
      ); } ulsb.append("
    "
    ); return ulsb.toString()+sb.toString(); } private static Map<String, String>[] getRowSpanColSpanMap(Sheet sheet) { Map<String, String> map0 = new HashMap<String, String>(); Map<String, String> map1 = new HashMap<String, String>(); int mergedNum = sheet.getNumMergedRegions(); CellRangeAddress range = null; for (int i = 0; i < mergedNum; i++) { range = sheet.getMergedRegion(i); int topRow = range.getFirstRow(); int topCol = range.getFirstColumn(); int bottomRow = range.getLastRow(); int bottomCol = range.getLastColumn(); map0.put(topRow + "," + topCol, bottomRow + "," + bottomCol); // System.out.println(topRow + "," + topCol + "," + bottomRow + "," + // bottomCol); int tempRow = topRow; while (tempRow <= bottomRow) { int tempCol = topCol; while (tempCol <= bottomCol) { map1.put(tempRow + "," + tempCol, ""); tempCol++; } tempRow++; } map1.remove(topRow + "," + topCol); } Map[] map = { map0, map1 }; return map; } private static String getCellValue1(Cell cell) { String result = new String(); switch (cell.getCellType()) { case NUMERIC:// 数字类型 result = "1"; break; case STRING:// String类型 result = "1"; break; case BLANK: result = ""; break; default: result = ""; break; } return result; } /** * 获取表格单元格Cell内容 * * @param cell * @return */ private static String getCellValue(Cell cell) { String result = new String(); switch (cell.getCellType()) { case NUMERIC:// 数字类型 if (DateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式 SimpleDateFormat sdf = null; if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) { sdf = new SimpleDateFormat("HH:mm"); } else {// 日期 sdf = new SimpleDateFormat("yyyy-MM-dd"); } Date date = cell.getDateCellValue(); result = sdf.format(date); } else if (cell.getCellStyle().getDataFormat() == 58) { // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); double value = cell.getNumericCellValue(); Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value); result = sdf.format(date); } else { double value = cell.getNumericCellValue(); CellStyle style = cell.getCellStyle(); DecimalFormat format = new DecimalFormat(); String temp = style.getDataFormatString(); // 单元格设置成常规 if (temp.equals("General")) { format.applyPattern("#"); } result = format.format(value); } break; case STRING:// String类型 result = cell.getRichStringCellValue().toString(); break; case BLANK: result = ""; break; default: result = ""; break; } return result; } /** * 处理表格样式 * * @param wb * @param sheet * @param sb */ private static void dealExcelStyle(Workbook wb, Sheet sheet, Cell cell, StringBuffer sb,Map<String,String> stylemap) { CellStyle cellStyle = cell.getCellStyle(); if (cellStyle != null) { HorizontalAlignment alignment = cellStyle.getAlignment(); // sb.append("align='" + convertAlignToHtml(alignment) + "' ");//单元格内容的水平对齐方式 VerticalAlignment verticalAlignment = cellStyle.getVerticalAlignment(); String _style = "vertical-align:"+convertVerticalAlignToHtml(verticalAlignment)+";"; if (wb instanceof XSSFWorkbook) { XSSFFont xf = ((XSSFCellStyle) cellStyle).getFont(); //short boldWeight = xf.getBoldweight(); short boldWeight = 400; String align = convertAlignToHtml(alignment); int columnWidth = sheet.getColumnWidth(cell.getColumnIndex()); _style +="font-weight:" + boldWeight + ";font-size: " + xf.getFontHeight() / 2 + "%;width:" + columnWidth + "px;text-align:" + align + ";"; XSSFColor xc = xf.getXSSFColor(); if (xc != null && !"".equals(xc)) { _style +="color:#" + xc.getARGBHex().substring(2) + ";"; } XSSFColor bgColor = (XSSFColor) cellStyle.getFillForegroundColorColor(); if (bgColor != null && !"".equals(bgColor)) { _style +="background-color:#" + bgColor.getARGBHex().substring(2) + ";"; // 背景颜色 } _style +=getBorderStyle(0, cellStyle.getBorderTop().getCode(),((XSSFCellStyle) cellStyle).getTopBorderXSSFColor()); _style +=getBorderStyle(1, cellStyle.getBorderRight().getCode(),((XSSFCellStyle) cellStyle).getRightBorderXSSFColor()); _style +=getBorderStyle(2, cellStyle.getBorderBottom().getCode(),((XSSFCellStyle) cellStyle).getBottomBorderXSSFColor()); _style +=getBorderStyle(3, cellStyle.getBorderLeft().getCode(),((XSSFCellStyle) cellStyle).getLeftBorderXSSFColor()); } else if (wb instanceof HSSFWorkbook) { HSSFFont hf = ((HSSFCellStyle) cellStyle).getFont(wb); short boldWeight = hf.getFontHeight(); short fontColor = hf.getColor(); HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette(); // 类HSSFPalette用于求的颜色的国际标准形式 HSSFColor hc = palette.getColor(fontColor); String align = convertAlignToHtml(alignment); int columnWidth = sheet.getColumnWidth(cell.getColumnIndex()); _style +="font-weight:" + boldWeight + ";font-size: " + hf.getFontHeight() / 2 + "%;text-align:" + align + ";width:" + columnWidth + "px;"; String fontColorStr = convertToStardColor(hc); if (fontColorStr != null && !"".equals(fontColorStr.trim())) { _style +="color:" + fontColorStr + ";"; // 字体颜色 } short bgColor = cellStyle.getFillForegroundColor(); hc = palette.getColor(bgColor); String bgColorStr = convertToStardColor(hc); if (bgColorStr != null && !"".equals(bgColorStr.trim())) { _style +="background-color:" + bgColorStr + ";"; // 背景颜色 } _style +=getBorderStyle(palette, 0, cellStyle.getBorderTop().getCode(), cellStyle.getTopBorderColor()); _style +=getBorderStyle(palette, 1, cellStyle.getBorderRight().getCode(), cellStyle.getRightBorderColor()); _style +=getBorderStyle(palette, 3, cellStyle.getBorderLeft().getCode(), cellStyle.getLeftBorderColor()); _style +=getBorderStyle(palette, 2, cellStyle.getBorderBottom().getCode(), cellStyle.getBottomBorderColor()); } String calssname=""; if(!stylemap.containsKey(_style)) { int count = stylemap.size(); calssname = "td"+count; stylemap.put(_style, calssname); }else { calssname = stylemap.get(_style); } if(!"".equals(calssname)) { sb.append("class='"+calssname+"'"); } } } /** * 单元格内容的水平对齐方式 * * @param alignment * @return */ private static String convertAlignToHtml(HorizontalAlignment alignment) { String align = "center"; switch (alignment) { case LEFT: align = "left"; break; case CENTER: align = "center"; break; case RIGHT: align = "right"; break; default: break; } return align; } /** * 单元格中内容的垂直排列方式 * * @param verticalAlignment * @return */ private static String convertVerticalAlignToHtml(VerticalAlignment verticalAlignment) { String valign = "middle"; switch (verticalAlignment) { case BOTTOM: valign = "bottom"; break; case CENTER: valign = "middle"; break; case TOP: valign = "top"; break; default: break; } return valign; } private static String convertToStardColor(HSSFColor hc) { StringBuffer sb = new StringBuffer(""); if (hc != null) { if (HSSFColor.HSSFColorPredefined.AUTOMATIC.getIndex() == hc.getIndex()) { return null; } sb.append("#"); for (int i = 0; i < hc.getTriplet().length; i++) { sb.append(fillWithZero(Integer.toHexString(hc.getTriplet()[i]))); } } return sb.toString(); } private static String fillWithZero(String str) { if (str != null && str.length() < 2) { return "0" + str; } return str; } static String[] bordesr = { "border-top:", "border-right:", "border-bottom:", "border-left:" }; static String[] borderStyles = { "solid ", "solid ", "solid ", "solid ", "solid ", "solid ", "solid ", "solid ", "solid ", "solid", "solid", "solid", "solid", "solid" }; private static String getBorderStyle(HSSFPalette palette, int b, short s, short t) { if (s == 0) return bordesr[b] + borderStyles[s] + "#d0d7e5 1px;"; String borderColorStr = convertToStardColor(palette.getColor(t)); borderColorStr = borderColorStr == null || borderColorStr.length() < 1 ? "#000000" : borderColorStr; return bordesr[b] + borderStyles[s] + borderColorStr + " 1px;"; } private static String getBorderStyle(int b, short s, XSSFColor xc) { if (s == 0) return bordesr[b] + borderStyles[s] + "#d0d7e5 1px;"; if (xc != null && !"".equals(xc)) { String borderColorStr = xc.getARGBHex();// t.getARGBHex(); borderColorStr = borderColorStr == null || borderColorStr.length() < 1 ? "#000000" : borderColorStr.substring(2); return bordesr[b] + borderStyles[s] + borderColorStr + " 1px;"; } return ""; } /* * @param content 生成的excel表格标签 * * @param htmlPath 生成的html文件地址 */ private static void writeFile(String content, String htmlPath, Map<String,String> stylemap,String name) { File file2 = new File(htmlPath); StringBuilder sb = new StringBuilder(); try { file2.createNewFile();// 创建文件 sb.append(""</span><span class="token operator">+</span>name<span class="token operator">+</span><span class="token string">""); sb.append("
    "); sb.append(content); sb.append("
    "
    ); sb.append(""); FileUtils.write(file2, sb.toString(),"UTF-8"); } catch (IOException e) { e.printStackTrace(); } } private static void writeFile1(String content, String htmlPath, Map<String,String> stylemap,String name) { File file2 = new File(htmlPath); StringBuilder sb = new StringBuilder(); try { file2.createNewFile();// 创建文件 sb.append(""</span><span class="token operator">+</span>name<span class="token operator">+</span><span class="token string">""); sb.append("
    "); sb.append(content); sb.append("
    "
    ); sb.append(""); FileUtils.write(file2, sb.toString(),"UTF-8"); } catch (IOException e) { e.printStackTrace(); } } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560
    • 561
    • 562
    • 563
    • 564
    • 565
    • 566
    • 567
    • 568
    • 569
    • 570
    • 571
    • 572
    • 573
    • 574
    • 575
    • 576
    • 577
    • 578
    • 579
    • 580
    • 581
    • 582
    • 583
    • 584
    • 585
    • 586
    • 587
    • 588
    • 589
    • 590
    • 591
    • 592
    • 593
    • 594
    • 595
    • 596
    • 597
    • 598
    • 599
    • 600
    • 601
    • 602
    • 603
    • 604
    • 605
    • 606
    • 607
    • 608
    • 609
    • 610
    • 611
    • 612
    • 613
    • 614
    • 615
    • 616
    • 617
    • 618
    • 619
    • 620
    • 621
    • 622
    • 623
    • 624
    • 625
    • 626
    • 627
    • 628
    • 629
    • 630
    • 631
    • 632
    • 633
    • 634
    • 635

    3、html样式解析

    3.1 加载图片-URL链接

    src设置需要加载的图片地址,alt是未加载到链接内容时展示的提示语

    <img src="http://www.baidu.com/images/xx.jpg" alt="暂未加载图像">
    
    • 1
    3.2 设置图片大小

    可以直接按比例设置100%;也可以直接按100px等比例设置

    width="图片宽度" height="图片高度"
    width=100%
    
    • 1
    • 2
    3.3 设置标题-底色&加粗

    hX是标题设置;background-color设置底色;font-weight:bold是加粗

    <h4 style="background-color:LightSkyBlue;font-weight:bold;">标题h4>
    
    • 1
    3.4 圆点样式

    设置为带圆点样式的语句

    <li id='li_1' οnclick='changetab(1)'>第一张li>
    
    • 1
    3.5 url链接显示为设置内容

    不直接展示该链接,显示为“点击查看内容”可点击跳转
    点击查看内容

    3.6 表格 – 一行三列
    <table border="1">
    <tr>
      <td>100td>
      <td>200td>
      <td>300td>
    tr>
    table>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    3.6 表格 – 两行三列
    <table border="1">
    <tr>
      <td> 1 td>
      <td> 2 td>
      <td> 3 td>
    tr>
    <tr>
      <td>400td>
      <td>500td>
      <td>600td>
    tr>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    3.7 段落

    段落正文中的具体内容

    <p>段落段落段落段落段落段落段落p>
    
    • 1

    4、时间工具类

    专供表格使用相关的时间工具类,解决格式转换的问题。

    //DateUtil
    public class DateUtil {
        public static boolean isCellDateFormatted(Cell cell)
        {
            if (cell == null) {
                return false;
            }
            boolean bDate = false;
    
            double d = cell.getNumericCellValue();
            if (isValidExcelDate(d)) {
                CellStyle style = cell.getCellStyle();
                if (style == null) {
                    return false;
                }
                int i = style.getDataFormat();
                String f = style.getDataFormatString();
                bDate = isADateFormat(i, f);
            }
            return bDate;
        }
    
        public static boolean isADateFormat(int formatIndex, String formatString)
        {
            if (isInternalDateFormat(formatIndex)) {
                return true;
            }
    
            if ((formatString == null) || (formatString.length() == 0)) {
                return false;
            }
    
            String fs = formatString;
            //添加以支持汉字格式wingzing
            fs = fs.replaceAll("[\"|\']","").replaceAll("[年|月|日|时|分|秒|毫秒|微秒]", "");
    
            fs = fs.replaceAll("\\\\-", "-");
    
            fs = fs.replaceAll("\\\\,", ",");
    
            fs = fs.replaceAll("\\\\.", ".");
    
            fs = fs.replaceAll("\\\\ ", " ");
    
            fs = fs.replaceAll(";@", "");
    
            fs = fs.replaceAll("^\\[\\$\\-.*?\\]", "");
    
            fs = fs.replaceAll("^\\[[a-zA-Z]+\\]", "");
    
            return (fs.matches("^[yYmMdDhHsS\\-/,. :]+[ampAMP/]*$"));
        }
    
        public static boolean isInternalDateFormat(int format)
        {
            switch (format) { case 14:
                case 15:
                case 16:
                case 17:
                case 18:
                case 19:
                case 20:
                case 21:
                case 22:
                case 45:
                case 46:
                case 47:
                    return true;
                case 23:
                case 24:
                case 25:
                case 26:
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                case 32:
                case 33:
                case 34:
                case 35:
                case 36:
                case 37:
                case 38:
                case 39:
                case 40:
                case 41:
                case 42:
                case 43:
                case 44: } return false;
        }
    
        public static boolean isValidExcelDate(double value)
        {
            return (value > -4.940656458412465E-324D);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    参考:
    https://www.runoob.com/html/html-colornames.html
    https://www.cnblogs.com/mythz/p/14177739.html
    https://blog.csdn.net/qq_33697094/article/details/122736603

  • 相关阅读:
    Java 基础 --- Java变量储存机制及参数传递
    力扣刷题记录8.1-----206. 反转链表
    从零实现Web框架Geo教程-Http基础-01
    【C++】C 语言 和 C++ 语言中 const 关键字分析 ( const 关键字左数右指原则 | C 语言中常量的原理和缺陷 | C++ 语言中常量原理 - 符号表存储常量 )
    数字ic验证门槛高吗?
    C++编码优化(1):条款1~4
    B_QuRT_User_Guide(33)
    Java TCP网络编程
    Kotlin中的字符串基本操作
    rabbitmq安装包部署erlang环境安装
  • 原文地址:https://blog.csdn.net/weixin_44436677/article/details/128178511