瀏覽代碼

修复Excel导出列数超过256错误

chenw 6 年之前
父節點
當前提交
85c06183a3

+ 4 - 0
bi-server/pom.xml

@@ -49,6 +49,10 @@
       <groupId>org.apache.poi</groupId>
       <artifactId>poi</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.poi</groupId>
+      <artifactId>poi-ooxml</artifactId>
+    </dependency>
 
     <dependency>
       <groupId>org.springframework.boot</groupId>

+ 2 - 2
bi-server/src/main/java/com/usoftchina/bi/server/controller/dashboard/DashboardsController.java

@@ -16,7 +16,7 @@ import com.usoftchina.bi.server.service.dashboard.DashboardsService;
 import com.usoftchina.bi.server.service.dashboard.DashboardsToChartsUtilService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
@@ -175,7 +175,7 @@ public class DashboardsController {
     @CheckToken
     @PostMapping("/dashboard/export")
     public void export(@RequestHeader String token, @RequestBody DashboardExportInfo dashboardExportInfo,  HttpServletResponse response) throws IOException {
-        HSSFWorkbook workbook = dashboardsService.export(dashboardExportInfo);
+        XSSFWorkbook workbook = dashboardsService.export(dashboardExportInfo);
         response.setContentType("application/vnd.ms-excel");
         response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(dashboardExportInfo.getDashboardName() + ".xlsx", "UTF-8"));
         OutputStream out = response.getOutputStream();

+ 28 - 32
bi-server/src/main/java/com/usoftchina/bi/server/service/dashboard/DashboardsService.java

@@ -29,24 +29,20 @@ import com.usoftchina.bi.server.service.chart.ChartsUtilService;
 import com.usoftchina.bi.core.utils.GetTokenDataUtil;
 import com.usoftchina.bi.server.service.common.MessageLogService;
 import com.usoftchina.bi.server.service.dataSource.DataConnectorService;
-import org.apache.poi.hssf.usermodel.*;
-import org.apache.poi.hssf.util.HSSFCellUtil;
 import org.apache.poi.hssf.util.HSSFColor;
-import org.apache.poi.ss.format.CellFormatType;
 import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 
 import java.io.IOException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.time.format.DateTimeFormatter;
 import java.util.*;
 
 @Service
@@ -441,8 +437,8 @@ public class DashboardsService {
      * @param dashboardExportInfo
      * @return
      */
-    public HSSFWorkbook export(DashboardExportInfo dashboardExportInfo){
-        HSSFWorkbook workbook = new HSSFWorkbook();
+    public XSSFWorkbook export(DashboardExportInfo dashboardExportInfo){
+        XSSFWorkbook workbook = new XSSFWorkbook();
         int dashboardId = dashboardExportInfo.getDashboardId();
         List<DataSourceExportInfo> dataSourceExportInfoList = dashboardExportInfo.getData();
         for (DataSourceExportInfo dataInfo : dataSourceExportInfoList) {
@@ -463,17 +459,17 @@ public class DashboardsService {
     }
 
 
-    private void createExcelSheet(HSSFWorkbook workbook, String sheetName, List<Map<String, Object>> dataList, List<ColumnRenameInfo> columnRenameInfo) {
+    private void createExcelSheet(XSSFWorkbook workbook, String sheetName, List<Map<String, Object>> dataList, List<ColumnRenameInfo> columnRenameInfo) {
         //添加sheet
-        HSSFSheet sheet = workbook.createSheet(sheetName);
-        HSSFRow row = sheet.createRow(0);
-        HSSFCell cell;
-        HSSFCellStyle cellStyle = getCellStyle(workbook);
+        XSSFSheet sheet = workbook.createSheet(sheetName);
+        XSSFRow row = sheet.createRow(0);
+        XSSFCell cell;
+        XSSFCellStyle cellStyle = getCellStyle(workbook);
         //设置标题
         for (int i = 0, len = columnRenameInfo.size(); i < len; i++) {
             cell = row.createCell(i);
             cell.setCellStyle(cellStyle);
-            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
+            cell.setCellType(CellType.STRING);
             cell.setCellValue(columnRenameInfo.get(i).getColumnLable());
         }
         //设置数据
@@ -482,10 +478,10 @@ public class DashboardsService {
             Map<String, Object> data = dataList.get(i);
             for (int j = 0, size = columnRenameInfo.size(); j < size; j++) {
                 cell = row.createCell(j);
-                int dataType = convertToCellType(columnRenameInfo.get(j).getColumnType());
+                CellType dataType = convertToCellType(columnRenameInfo.get(j).getColumnType());
                 Object value = data.get(columnRenameInfo.get(j).getColumnName());
-                if (9 == dataType) {
-                    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
+                if (CellType.BLANK.equals(dataType)) {
+                    cell.setCellType(CellType.NUMERIC);
                     Date dataValue = null;
                     try {
                         dataValue = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(String.valueOf(value));
@@ -502,36 +498,36 @@ public class DashboardsService {
 
     }
 
-    private int convertToCellType(String columnType) {
+    private CellType convertToCellType(String columnType) {
         switch (columnType) {
             case "categorical":
-                return HSSFCell.CELL_TYPE_STRING;
+                return CellType.STRING;
             case "Timestamp":
-                return 9;
+                return CellType.BLANK;
             case "BigDecimal":
-                return HSSFCell.CELL_TYPE_NUMERIC;
+                return CellType.NUMERIC;
             case "scale":
-                return HSSFCell.CELL_TYPE_NUMERIC;
+                return CellType.NUMERIC;
             default:
-                return HSSFCell.CELL_TYPE_STRING;
+                return CellType.STRING;
         }
     }
 
-    private HSSFCellStyle getCellStyle(HSSFWorkbook workbook) {
-        HSSFCellStyle style = workbook.createCellStyle();
-        Font font = workbook.createFont();
+    private XSSFCellStyle getCellStyle(XSSFWorkbook workbook) {
+        XSSFCellStyle style = workbook.createCellStyle();
+        XSSFFont font = workbook.createFont();
         font.setFontName("仿宋_GB2312");// 字体
         font.setFontHeightInPoints((short) 12);// 字号
-        font.setBoldweight(new Short("8"));
+        font.setBold(true);
         font.setColor((short)64);// 颜色
         style.setFont(font);
-        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
-        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
+        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+        style.setAlignment(HorizontalAlignment.CENTER);
         style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
-        style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
-        style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
-        style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
-        style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
+        style.setBorderBottom(BorderStyle.MEDIUM);
+        style.setBorderLeft(BorderStyle.MEDIUM);
+        style.setBorderRight(BorderStyle.MEDIUM);
+        style.setBorderTop(BorderStyle.MEDIUM);
         return style;
     }
 

+ 7 - 1
pom.xml

@@ -37,7 +37,8 @@
     <swagger.version>2.7.0</swagger.version>
     <mysql.version>6.0.6</mysql.version>
     <jwt.version>3.1.0</jwt.version>
-    <poi.version>3.9</poi.version>
+    <poi.version>3.17</poi.version>
+    <poi-ooxml.version>3.17</poi-ooxml.version>
   </properties>
 
   <repositories>
@@ -166,6 +167,11 @@
         <artifactId>poi</artifactId>
         <version>${poi.version}</version>
       </dependency>
+      <dependency>
+        <groupId>org.apache.poi</groupId>
+        <artifactId>poi-ooxml</artifactId>
+        <version>${poi-ooxml.version}</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>