Commit 297711911b323bc050f5dde36ca20fcdf48ab11e

Authored by 徐烜
1 parent fa469781

Update

Showing 22 changed files with 613 additions and 223 deletions

Too many changes to show.

To preserve performance only 22 of 24 files are displayed.

... ... @@ -106,11 +106,11 @@
106 106 <artifactId>janino</artifactId>
107 107 </dependency>
108 108  
109   - <dependency>
110   - <groupId>org.apache.poi</groupId>
111   - <artifactId>poi</artifactId>
112   - <version>3.13</version>
113   - </dependency>
  109 + <dependency>
  110 + <groupId>org.apache.poi</groupId>
  111 + <artifactId>poi-ooxml</artifactId>
  112 + <version>3.13</version>
  113 + </dependency>
114 114  
115 115 <dependency>
116 116 <groupId>com.google.guava</groupId>
... ...
src/main/java/com/bsth/controller/schedule/BController.java
... ... @@ -6,11 +6,11 @@ import com.bsth.entity.schedule.BEntity;
6 6 import com.bsth.entity.sys.SysUser;
7 7 import com.bsth.service.schedule.BService;
8 8 import com.bsth.service.schedule.exception.ScheduleException;
  9 +import com.bsth.service.schedule.utils.DataToolsFile;
9 10 import com.bsth.service.sys.SysUserService;
10 11 import com.google.common.base.Splitter;
11   -import jxl.Sheet;
12   -import jxl.Workbook;
13 12 import org.apache.commons.lang3.StringUtils;
  13 +import org.apache.poi.ss.usermodel.Workbook;
14 14 import org.springframework.beans.factory.annotation.Autowired;
15 15 import org.springframework.data.domain.PageRequest;
16 16 import org.springframework.data.domain.Sort;
... ... @@ -154,20 +154,24 @@ public class BController&lt;T, ID extends Serializable&gt; {
154 154 public Map<String, Object> uploadFile(MultipartFile file) {
155 155 Map<String, Object> rtn = new HashMap<>();
156 156 try {
157   - File file1 = bService.uploadFile(file.getOriginalFilename(), file.getBytes());
  157 + DataToolsFile dataToolsFile = bService.uploadFile(file.getOriginalFilename(), file.getBytes());
158 158 // excel文件名
159   - String fileName = file1.getAbsolutePath();
  159 + String fileName = dataToolsFile.getFile().getAbsolutePath();
  160 + Workbook wb = dataToolsFile.getFileType().getWorkBook(dataToolsFile.getFile());
  161 +
160 162 // excel文件sheet
161 163 List<String> sheetnames = new ArrayList<>();
162   - Workbook book = Workbook.getWorkbook(file1);
163   - for (Sheet sheet : book.getSheets()) {
164   - sheetnames.add(sheet.getName());
  164 + for (int i = 0; i < wb.getNumberOfSheets(); i ++) {
  165 + sheetnames.add(wb.getSheetAt(i).getSheetName());
165 166 }
166 167  
  168 + wb.close();
  169 +
167 170 rtn.put("status", ResponseCode.SUCCESS);
168 171 rtn.put("filename", fileName);
169 172 rtn.put("sheetnames", StringUtils.join(sheetnames, ","));
170 173 } catch (Exception exp) {
  174 + exp.printStackTrace();
171 175 rtn.put("status", ResponseCode.ERROR);
172 176 rtn.put("msg", exp.getMessage());
173 177 }
... ... @@ -202,9 +206,9 @@ public class BController&lt;T, ID extends Serializable&gt; {
202 206 Map<String, Object> rtn = new HashMap<>();
203 207  
204 208 try {
205   - File file1 = bService.uploadFile(file.getOriginalFilename(), file.getBytes());
  209 + DataToolsFile dataToolsFile = bService.uploadFile(file.getOriginalFilename(), file.getBytes());
206 210 Map<String, Object> params = new HashMap<>();
207   - bService.importData(file1, params);
  211 + bService.importData(dataToolsFile.getFile(), params);
208 212  
209 213 rtn.put("status", ResponseCode.SUCCESS);
210 214 rtn.put("msg", "上传&导入文件成功");
... ... @@ -220,16 +224,16 @@ public class BController&lt;T, ID extends Serializable&gt; {
220 224 @RequestMapping(value = "/exportFile", method = RequestMethod.GET)
221 225 public void exportFile(HttpServletResponse response,
222 226 @RequestParam Map<String, Object> params) throws Exception {
223   - File file = bService.exportData(params);
  227 + DataToolsFile dataToolsFile = bService.exportData(params);
224 228 // 流输出导出文件
225 229 response.setHeader("content-type", "application/octet-stream");
226   - response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
  230 + response.setHeader("Content-Disposition", "attachment; filename=" + dataToolsFile.getFile().getName());
227 231 response.setContentType("application/octet-stream");
228 232  
229 233 OutputStream os = response.getOutputStream();
230 234 BufferedOutputStream bos = new BufferedOutputStream(os);
231 235  
232   - InputStream is = new FileInputStream(file);
  236 + InputStream is = new FileInputStream(dataToolsFile.getFile());
233 237 BufferedInputStream bis = new BufferedInputStream(is);
234 238  
235 239 int length = 0;
... ...
src/main/java/com/bsth/service/schedule/datatools/CarConfigInfoDataToolsImpl.java
1 1 package com.bsth.service.schedule.datatools;
2 2  
3 3 import com.bsth.service.schedule.exception.ScheduleException;
4   -import com.bsth.service.schedule.utils.DataToolsProperties;
5   -import com.bsth.service.schedule.utils.DataToolsService;
6   -import jxl.Cell;
7   -import jxl.Sheet;
8   -import jxl.Workbook;
  4 +import com.bsth.service.schedule.utils.*;
9 5 import jxl.write.Label;
10 6 import jxl.write.WritableSheet;
11 7 import jxl.write.WritableWorkbook;
  8 +import org.apache.poi.ss.usermodel.Sheet;
  9 +import org.apache.poi.ss.usermodel.Workbook;
12 10 import org.slf4j.Logger;
13 11 import org.slf4j.LoggerFactory;
14 12 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -39,26 +37,45 @@ public class CarConfigInfoDataToolsImpl implements DataToolsService {
39 37 private DataToolsProperties dataToolsProperties;
40 38  
41 39 @Override
42   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  40 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
43 41 try {
44   - // 对上传的excel文件做处理,将第一个sheet名字设定为工作表1
45   - File file = dataToolsService.uploadFile(filename, filedata);
46   - Workbook workbook = Workbook.getWorkbook(file);
47   - Sheet sheet = workbook.getSheet(0);
  42 + // 对上传的excel文件做处理
  43 + // 将第一个sheet保存成一个xls文件
  44 + DataToolsFile dataToolsFile = dataToolsService.uploadFile(filename, filedata);
  45 + File file = dataToolsFile.getFile();
  46 +
  47 + // poi api
  48 + Workbook poi_workbook = dataToolsFile.getFileType().getWorkBook(file);
  49 + Sheet poi_sheet = poi_workbook.getSheetAt(0); // 第一个sheet
  50 + int rowNum = poi_sheet.getLastRowNum(); // 获取总共多少行
  51 + if (rowNum < 0) {
  52 + throw new RuntimeException("表格内容为空!");
  53 + }
  54 + int colNum = poi_sheet.getRow(0).getLastCellNum(); // 获取总共多少列,以第一行为主
48 55  
  56 + // jxl api
49 57 File fileCal = new File(file.getAbsolutePath() + "_sheetChange.xls");
50   - WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal);
  58 + WritableWorkbook writableWorkbook = jxl.Workbook.createWorkbook(fileCal);
51 59 WritableSheet writableSheet = writableWorkbook.createSheet("工作表1", 0);
52   - for (int i = 0; i < sheet.getRows(); i++) {
53   - Cell[] cells = sheet.getRow(i);
54   - for (int j = 0; j < cells.length; j++) {
55   - writableSheet.addCell(new Label(j, i, cells[j].getContents()));
  60 +
  61 + for (int i = 0; i <= rowNum; i++) {
  62 + for (int j = 0; j <= colNum; j++) {
  63 + // poi读
  64 + String cellContent = PoiUtils.getStringValueFromCell(poi_sheet.getRow(i).getCell(j));
  65 + // jxl写
  66 + writableSheet.addCell(new Label(j, i, cellContent));
56 67 }
  68 +
57 69 }
58 70 writableWorkbook.write();
59 71 writableWorkbook.close();
  72 + poi_workbook.close();
  73 +
  74 + DataToolsFile dataToolsFile1 = new DataToolsFile();
  75 + dataToolsFile1.setFile(fileCal);
  76 + dataToolsFile1.setFileType(DataToolsFileType.XLS);
60 77  
61   - return fileCal;
  78 + return dataToolsFile1;
62 79  
63 80 } catch (Exception exp) {
64 81 throw new ScheduleException(exp);
... ... @@ -96,7 +113,7 @@ public class CarConfigInfoDataToolsImpl implements DataToolsService {
96 113 }
97 114  
98 115 @Override
99   - public File exportData(Map<String, Object> params) throws ScheduleException {
  116 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
100 117 try {
101 118 LOGGER.info("//---------------- 导出车辆配置信息 start... ----------------//");
102 119 // 创建ktr转换所需参数
... ... @@ -110,7 +127,7 @@ public class CarConfigInfoDataToolsImpl implements DataToolsService {
110 127  
111 128 ktrParms.putAll(params);
112 129  
113   - File file = dataToolsService.exportData(ktrParms);
  130 + DataToolsFile file = dataToolsService.exportData(ktrParms);
114 131  
115 132 LOGGER.info("//---------------- 导出车辆配置信息 success... ----------------//");
116 133  
... ...
src/main/java/com/bsth/service/schedule/datatools/CarsDataToolsImpl.java
1 1 package com.bsth.service.schedule.datatools;
2 2  
3 3 import com.bsth.service.schedule.exception.ScheduleException;
4   -import com.bsth.service.schedule.utils.DataToolsProperties;
5   -import com.bsth.service.schedule.utils.DataToolsService;
6   -import jxl.Cell;
7   -import jxl.Sheet;
8   -import jxl.Workbook;
  4 +import com.bsth.service.schedule.utils.*;
9 5 import jxl.write.Label;
10 6 import jxl.write.WritableSheet;
11 7 import jxl.write.WritableWorkbook;
... ... @@ -39,26 +35,45 @@ public class CarsDataToolsImpl implements DataToolsService {
39 35 private DataToolsProperties dataToolsProperties;
40 36  
41 37 @Override
42   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  38 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
43 39 try {
44   - // 对上传的excel文件做处理,将第一个sheet名字设定为工作表1
45   - File file = dataToolsService.uploadFile(filename, filedata);
46   - Workbook workbook = Workbook.getWorkbook(file);
47   - Sheet sheet = workbook.getSheet(0);
  40 + // 对上传的excel文件做处理
  41 + // 将第一个sheet保存成一个xls文件
  42 + DataToolsFile dataToolsFile = dataToolsService.uploadFile(filename, filedata);
  43 + File file = dataToolsFile.getFile();
  44 +
  45 + // poi api
  46 + org.apache.poi.ss.usermodel.Workbook poi_workbook = dataToolsFile.getFileType().getWorkBook(file);
  47 + org.apache.poi.ss.usermodel.Sheet poi_sheet = poi_workbook.getSheetAt(0); // 第一个sheet
  48 + int rowNum = poi_sheet.getLastRowNum(); // 获取总共多少行
  49 + if (rowNum < 0) {
  50 + throw new RuntimeException("表格内容为空!");
  51 + }
  52 + int colNum = poi_sheet.getRow(0).getLastCellNum(); // 获取总共多少列,以第一行为主
48 53  
  54 + // jxl api
49 55 File fileCal = new File(file.getAbsolutePath() + "_sheetChange.xls");
50   - WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal);
  56 + WritableWorkbook writableWorkbook = jxl.Workbook.createWorkbook(fileCal);
51 57 WritableSheet writableSheet = writableWorkbook.createSheet("工作表1", 0);
52   - for (int i = 0; i < sheet.getRows(); i++) {
53   - Cell[] cells = sheet.getRow(i);
54   - for (int j = 0; j < cells.length; j++) {
55   - writableSheet.addCell(new Label(j, i, cells[j].getContents()));
  58 +
  59 + for (int i = 0; i <= rowNum; i++) {
  60 + for (int j = 0; j <= colNum; j++) {
  61 + // poi读
  62 + String cellContent = PoiUtils.getStringValueFromCell(poi_sheet.getRow(i).getCell(j));
  63 + // jxl写
  64 + writableSheet.addCell(new Label(j, i, cellContent));
56 65 }
  66 +
57 67 }
58 68 writableWorkbook.write();
59 69 writableWorkbook.close();
  70 + poi_workbook.close();
  71 +
  72 + DataToolsFile dataToolsFile1 = new DataToolsFile();
  73 + dataToolsFile1.setFile(fileCal);
  74 + dataToolsFile1.setFileType(DataToolsFileType.XLS);
60 75  
61   - return fileCal;
  76 + return dataToolsFile1;
62 77  
63 78 } catch (Exception exp) {
64 79 throw new ScheduleException(exp);
... ... @@ -94,7 +109,7 @@ public class CarsDataToolsImpl implements DataToolsService {
94 109 }
95 110  
96 111 @Override
97   - public File exportData(Map<String, Object> params) throws ScheduleException {
  112 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
98 113 try {
99 114 LOGGER.info("//---------------- 导出车辆基础信息 start... ----------------//");
100 115 // 创建ktr转换所需参数
... ... @@ -106,7 +121,7 @@ public class CarsDataToolsImpl implements DataToolsService {
106 121 ktrParms.put("transpath", ktrFile.getAbsolutePath());
107 122 ktrParms.put("filename", "车辆基础信息_download-");
108 123  
109   - File file = dataToolsService.exportData(ktrParms);
  124 + DataToolsFile file = dataToolsService.exportData(ktrParms);
110 125  
111 126 LOGGER.info("//---------------- 导出车辆基础信息 success... ----------------//");
112 127  
... ...
src/main/java/com/bsth/service/schedule/datatools/EmployeeConfigInfoDataToolsImpl.java
1 1 package com.bsth.service.schedule.datatools;
2 2  
3 3 import com.bsth.service.schedule.exception.ScheduleException;
4   -import com.bsth.service.schedule.utils.DataToolsProperties;
5   -import com.bsth.service.schedule.utils.DataToolsService;
6   -import jxl.Cell;
7   -import jxl.Sheet;
8   -import jxl.Workbook;
  4 +import com.bsth.service.schedule.utils.*;
9 5 import jxl.write.Label;
10 6 import jxl.write.WritableSheet;
11 7 import jxl.write.WritableWorkbook;
... ... @@ -39,26 +35,45 @@ public class EmployeeConfigInfoDataToolsImpl implements DataToolsService {
39 35 private DataToolsProperties dataToolsProperties;
40 36  
41 37 @Override
42   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  38 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
43 39 try {
44   - // 对上传的excel文件做处理,将第一个sheet名字设定为工作表1
45   - File file = dataToolsService.uploadFile(filename, filedata);
46   - Workbook workbook = Workbook.getWorkbook(file);
47   - Sheet sheet = workbook.getSheet(0);
  40 + // 对上传的excel文件做处理
  41 + // 将第一个sheet保存成一个xls文件
  42 + DataToolsFile dataToolsFile = dataToolsService.uploadFile(filename, filedata);
  43 + File file = dataToolsFile.getFile();
  44 +
  45 + // poi api
  46 + org.apache.poi.ss.usermodel.Workbook poi_workbook = dataToolsFile.getFileType().getWorkBook(file);
  47 + org.apache.poi.ss.usermodel.Sheet poi_sheet = poi_workbook.getSheetAt(0); // 第一个sheet
  48 + int rowNum = poi_sheet.getLastRowNum(); // 获取总共多少行
  49 + if (rowNum < 0) {
  50 + throw new RuntimeException("表格内容为空!");
  51 + }
  52 + int colNum = poi_sheet.getRow(0).getLastCellNum(); // 获取总共多少列,以第一行为主
48 53  
  54 + // jxl api
49 55 File fileCal = new File(file.getAbsolutePath() + "_sheetChange.xls");
50   - WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal);
  56 + WritableWorkbook writableWorkbook = jxl.Workbook.createWorkbook(fileCal);
51 57 WritableSheet writableSheet = writableWorkbook.createSheet("工作表1", 0);
52   - for (int i = 0; i < sheet.getRows(); i++) {
53   - Cell[] cells = sheet.getRow(i);
54   - for (int j = 0; j < cells.length; j++) {
55   - writableSheet.addCell(new Label(j, i, cells[j].getContents()));
  58 +
  59 + for (int i = 0; i <= rowNum; i++) {
  60 + for (int j = 0; j <= colNum; j++) {
  61 + // poi读
  62 + String cellContent = PoiUtils.getStringValueFromCell(poi_sheet.getRow(i).getCell(j));
  63 + // jxl写
  64 + writableSheet.addCell(new Label(j, i, cellContent));
56 65 }
  66 +
57 67 }
58 68 writableWorkbook.write();
59 69 writableWorkbook.close();
  70 + poi_workbook.close();
  71 +
  72 + DataToolsFile dataToolsFile1 = new DataToolsFile();
  73 + dataToolsFile1.setFile(fileCal);
  74 + dataToolsFile1.setFileType(DataToolsFileType.XLS);
60 75  
61   - return fileCal;
  76 + return dataToolsFile1;
62 77  
63 78 } catch (Exception exp) {
64 79 throw new ScheduleException(exp);
... ... @@ -96,7 +111,7 @@ public class EmployeeConfigInfoDataToolsImpl implements DataToolsService {
96 111 }
97 112  
98 113 @Override
99   - public File exportData(Map<String, Object> params) throws ScheduleException {
  114 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
100 115 try {
101 116 LOGGER.info("//---------------- 导出人员配置信息 start... ----------------//");
102 117 // 创建ktr转换所需参数
... ... @@ -110,7 +125,7 @@ public class EmployeeConfigInfoDataToolsImpl implements DataToolsService {
110 125  
111 126 ktrParms.putAll(params);
112 127  
113   - File file = dataToolsService.exportData(ktrParms);
  128 + DataToolsFile file = dataToolsService.exportData(ktrParms);
114 129  
115 130 LOGGER.info("//---------------- 导出人员配置信息 success... ----------------//");
116 131  
... ...
src/main/java/com/bsth/service/schedule/datatools/EmployeeDataToolsImpl.java
1 1 package com.bsth.service.schedule.datatools;
2 2  
3 3 import com.bsth.service.schedule.exception.ScheduleException;
4   -import com.bsth.service.schedule.utils.DataToolsProperties;
5   -import com.bsth.service.schedule.utils.DataToolsService;
6   -import jxl.Cell;
7   -import jxl.Sheet;
8   -import jxl.Workbook;
  4 +import com.bsth.service.schedule.utils.*;
9 5 import jxl.write.Label;
10 6 import jxl.write.WritableSheet;
11 7 import jxl.write.WritableWorkbook;
... ... @@ -39,26 +35,45 @@ public class EmployeeDataToolsImpl implements DataToolsService {
39 35 private DataToolsProperties dataToolsProperties;
40 36  
41 37 @Override
42   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  38 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
43 39 try {
44   - // 对上传的excel文件做处理,将第一个sheet名字设定为工作表1
45   - File file = dataToolsService.uploadFile(filename, filedata);
46   - Workbook workbook = Workbook.getWorkbook(file);
47   - Sheet sheet = workbook.getSheet(0);
  40 + // 对上传的excel文件做处理
  41 + // 将第一个sheet保存成一个xls文件
  42 + DataToolsFile dataToolsFile = dataToolsService.uploadFile(filename, filedata);
  43 + File file = dataToolsFile.getFile();
  44 +
  45 + // poi api
  46 + org.apache.poi.ss.usermodel.Workbook poi_workbook = dataToolsFile.getFileType().getWorkBook(file);
  47 + org.apache.poi.ss.usermodel.Sheet poi_sheet = poi_workbook.getSheetAt(0); // 第一个sheet
  48 + int rowNum = poi_sheet.getLastRowNum(); // 获取总共多少行
  49 + if (rowNum < 0) {
  50 + throw new RuntimeException("表格内容为空!");
  51 + }
  52 + int colNum = poi_sheet.getRow(0).getLastCellNum(); // 获取总共多少列,以第一行为主
48 53  
  54 + // jxl api
49 55 File fileCal = new File(file.getAbsolutePath() + "_sheetChange.xls");
50   - WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal);
  56 + WritableWorkbook writableWorkbook = jxl.Workbook.createWorkbook(fileCal);
51 57 WritableSheet writableSheet = writableWorkbook.createSheet("工作表1", 0);
52   - for (int i = 0; i < sheet.getRows(); i++) {
53   - Cell[] cells = sheet.getRow(i);
54   - for (int j = 0; j < cells.length; j++) {
55   - writableSheet.addCell(new Label(j, i, cells[j].getContents()));
  58 +
  59 + for (int i = 0; i <= rowNum; i++) {
  60 + for (int j = 0; j <= colNum; j++) {
  61 + // poi读
  62 + String cellContent = PoiUtils.getStringValueFromCell(poi_sheet.getRow(i).getCell(j));
  63 + // jxl写
  64 + writableSheet.addCell(new Label(j, i, cellContent));
56 65 }
  66 +
57 67 }
58 68 writableWorkbook.write();
59 69 writableWorkbook.close();
  70 + poi_workbook.close();
  71 +
  72 + DataToolsFile dataToolsFile1 = new DataToolsFile();
  73 + dataToolsFile1.setFile(fileCal);
  74 + dataToolsFile1.setFileType(DataToolsFileType.XLS);
60 75  
61   - return fileCal;
  76 + return dataToolsFile1;
62 77  
63 78 } catch (Exception exp) {
64 79 throw new ScheduleException(exp);
... ... @@ -94,7 +109,7 @@ public class EmployeeDataToolsImpl implements DataToolsService {
94 109 }
95 110  
96 111 @Override
97   - public File exportData(Map<String, Object> params) throws ScheduleException {
  112 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
98 113 try {
99 114 LOGGER.info("//---------------- 导出人员基础信息 start... ----------------//");
100 115 // 创建ktr转换所需参数
... ... @@ -106,7 +121,7 @@ public class EmployeeDataToolsImpl implements DataToolsService {
106 121 ktrParms.put("transpath", ktrFile.getAbsolutePath());
107 122 ktrParms.put("filename", "人员基础信息_download-");
108 123  
109   - File file = dataToolsService.exportData(ktrParms);
  124 + DataToolsFile file = dataToolsService.exportData(ktrParms);
110 125  
111 126 LOGGER.info("//---------------- 导出人员基础信息 success... ----------------//");
112 127  
... ...
src/main/java/com/bsth/service/schedule/datatools/GuideboardInfoDataToolsImpl.java
1 1 package com.bsth.service.schedule.datatools;
2 2  
3 3 import com.bsth.service.schedule.exception.ScheduleException;
4   -import com.bsth.service.schedule.utils.DataToolsProperties;
5   -import com.bsth.service.schedule.utils.DataToolsService;
6   -import jxl.Cell;
7   -import jxl.Sheet;
8   -import jxl.Workbook;
  4 +import com.bsth.service.schedule.utils.*;
9 5 import jxl.write.Label;
10 6 import jxl.write.WritableSheet;
11 7 import jxl.write.WritableWorkbook;
... ... @@ -39,26 +35,45 @@ public class GuideboardInfoDataToolsImpl implements DataToolsService {
39 35 private DataToolsProperties dataToolsProperties;
40 36  
41 37 @Override
42   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  38 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
43 39 try {
44   - // 对上传的excel文件做处理,将第一个sheet名字设定为工作表1
45   - File file = dataToolsService.uploadFile(filename, filedata);
46   - Workbook workbook = Workbook.getWorkbook(file);
47   - Sheet sheet = workbook.getSheet(0);
  40 + // 对上传的excel文件做处理
  41 + // 将第一个sheet保存成一个xls文件
  42 + DataToolsFile dataToolsFile = dataToolsService.uploadFile(filename, filedata);
  43 + File file = dataToolsFile.getFile();
  44 +
  45 + // poi api
  46 + org.apache.poi.ss.usermodel.Workbook poi_workbook = dataToolsFile.getFileType().getWorkBook(file);
  47 + org.apache.poi.ss.usermodel.Sheet poi_sheet = poi_workbook.getSheetAt(0); // 第一个sheet
  48 + int rowNum = poi_sheet.getLastRowNum(); // 获取总共多少行
  49 + if (rowNum < 0) {
  50 + throw new RuntimeException("表格内容为空!");
  51 + }
  52 + int colNum = poi_sheet.getRow(0).getLastCellNum(); // 获取总共多少列,以第一行为主
48 53  
  54 + // jxl api
49 55 File fileCal = new File(file.getAbsolutePath() + "_sheetChange.xls");
50   - WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal);
  56 + WritableWorkbook writableWorkbook = jxl.Workbook.createWorkbook(fileCal);
51 57 WritableSheet writableSheet = writableWorkbook.createSheet("工作表1", 0);
52   - for (int i = 0; i < sheet.getRows(); i++) {
53   - Cell[] cells = sheet.getRow(i);
54   - for (int j = 0; j < cells.length; j++) {
55   - writableSheet.addCell(new Label(j, i, cells[j].getContents()));
  58 +
  59 + for (int i = 0; i <= rowNum; i++) {
  60 + for (int j = 0; j <= colNum; j++) {
  61 + // poi读
  62 + String cellContent = PoiUtils.getStringValueFromCell(poi_sheet.getRow(i).getCell(j));
  63 + // jxl写
  64 + writableSheet.addCell(new Label(j, i, cellContent));
56 65 }
  66 +
57 67 }
58 68 writableWorkbook.write();
59 69 writableWorkbook.close();
  70 + poi_workbook.close();
  71 +
  72 + DataToolsFile dataToolsFile1 = new DataToolsFile();
  73 + dataToolsFile1.setFile(fileCal);
  74 + dataToolsFile1.setFileType(DataToolsFileType.XLS);
60 75  
61   - return fileCal;
  76 + return dataToolsFile1;
62 77  
63 78 } catch (Exception exp) {
64 79 throw new ScheduleException(exp);
... ... @@ -96,7 +111,7 @@ public class GuideboardInfoDataToolsImpl implements DataToolsService {
96 111 }
97 112  
98 113 @Override
99   - public File exportData(Map<String, Object> params) throws ScheduleException {
  114 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
100 115 try {
101 116 LOGGER.info("//---------------- 导出路牌信息 start... ----------------//");
102 117 // 创建ktr转换所需参数
... ... @@ -110,7 +125,7 @@ public class GuideboardInfoDataToolsImpl implements DataToolsService {
110 125  
111 126 ktrParms.putAll(params);
112 127  
113   - File file = dataToolsService.exportData(ktrParms);
  128 + DataToolsFile file = dataToolsService.exportData(ktrParms);
114 129  
115 130 LOGGER.info("//---------------- 导出路牌信息 success... ----------------//");
116 131  
... ...
src/main/java/com/bsth/service/schedule/datatools/ScheduleRule1FlatDataToolsImpl.java
1 1 package com.bsth.service.schedule.datatools;
2 2  
3 3 import com.bsth.service.schedule.exception.ScheduleException;
4   -import com.bsth.service.schedule.utils.DataToolsProperties;
5   -import com.bsth.service.schedule.utils.DataToolsService;
6   -import jxl.Cell;
7   -import jxl.Sheet;
8   -import jxl.Workbook;
  4 +import com.bsth.service.schedule.utils.*;
9 5 import jxl.write.Label;
10 6 import jxl.write.WritableSheet;
11 7 import jxl.write.WritableWorkbook;
... ... @@ -39,26 +35,45 @@ public class ScheduleRule1FlatDataToolsImpl implements DataToolsService {
39 35 private DataToolsProperties dataToolsProperties;
40 36  
41 37 @Override
42   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  38 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
43 39 try {
44   - // 对上传的excel文件做处理,将第一个sheet名字设定为工作表1
45   - File file = dataToolsService.uploadFile(filename, filedata);
46   - Workbook workbook = Workbook.getWorkbook(file);
47   - Sheet sheet = workbook.getSheet(0);
  40 + // 对上传的excel文件做处理
  41 + // 将第一个sheet保存成一个xls文件
  42 + DataToolsFile dataToolsFile = dataToolsService.uploadFile(filename, filedata);
  43 + File file = dataToolsFile.getFile();
  44 +
  45 + // poi api
  46 + org.apache.poi.ss.usermodel.Workbook poi_workbook = dataToolsFile.getFileType().getWorkBook(file);
  47 + org.apache.poi.ss.usermodel.Sheet poi_sheet = poi_workbook.getSheetAt(0); // 第一个sheet
  48 + int rowNum = poi_sheet.getLastRowNum(); // 获取总共多少行
  49 + if (rowNum < 0) {
  50 + throw new RuntimeException("表格内容为空!");
  51 + }
  52 + int colNum = poi_sheet.getRow(0).getLastCellNum(); // 获取总共多少列,以第一行为主
48 53  
  54 + // jxl api
49 55 File fileCal = new File(file.getAbsolutePath() + "_sheetChange.xls");
50   - WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal);
  56 + WritableWorkbook writableWorkbook = jxl.Workbook.createWorkbook(fileCal);
51 57 WritableSheet writableSheet = writableWorkbook.createSheet("工作表1", 0);
52   - for (int i = 0; i < sheet.getRows(); i++) {
53   - Cell[] cells = sheet.getRow(i);
54   - for (int j = 0; j < cells.length; j++) {
55   - writableSheet.addCell(new Label(j, i, cells[j].getContents()));
  58 +
  59 + for (int i = 0; i <= rowNum; i++) {
  60 + for (int j = 0; j <= colNum; j++) {
  61 + // poi读
  62 + String cellContent = PoiUtils.getStringValueFromCell(poi_sheet.getRow(i).getCell(j));
  63 + // jxl写
  64 + writableSheet.addCell(new Label(j, i, cellContent));
56 65 }
  66 +
57 67 }
58 68 writableWorkbook.write();
59 69 writableWorkbook.close();
  70 + poi_workbook.close();
  71 +
  72 + DataToolsFile dataToolsFile1 = new DataToolsFile();
  73 + dataToolsFile1.setFile(fileCal);
  74 + dataToolsFile1.setFileType(DataToolsFileType.XLS);
60 75  
61   - return fileCal;
  76 + return dataToolsFile1;
62 77  
63 78 } catch (Exception exp) {
64 79 throw new ScheduleException(exp);
... ... @@ -96,7 +111,7 @@ public class ScheduleRule1FlatDataToolsImpl implements DataToolsService {
96 111 }
97 112  
98 113 @Override
99   - public File exportData(Map<String, Object> params) throws ScheduleException {
  114 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
100 115 try {
101 116 LOGGER.info("//---------------- 导出排版规则信息 start... ----------------//");
102 117 // 创建ktr转换所需参数
... ... @@ -110,7 +125,7 @@ public class ScheduleRule1FlatDataToolsImpl implements DataToolsService {
110 125  
111 126 ktrParms.putAll(params);
112 127  
113   - File file = dataToolsService.exportData(ktrParms);
  128 + DataToolsFile file = dataToolsService.exportData(ktrParms);
114 129  
115 130 LOGGER.info("//---------------- 导出排版规则信息 success... ----------------//");
116 131  
... ...
src/main/java/com/bsth/service/schedule/datatools/TTInfoDetailDataToolsImpl.java
1 1 package com.bsth.service.schedule.datatools;
2 2  
3 3 import com.bsth.service.schedule.exception.ScheduleException;
4   -import com.bsth.service.schedule.utils.DataToolsProperties;
5   -import com.bsth.service.schedule.utils.DataToolsService;
6   -import jxl.Cell;
  4 +import com.bsth.service.schedule.utils.*;
7 5 import jxl.Sheet;
8 6 import jxl.Workbook;
9 7 import jxl.write.Label;
10 8 import jxl.write.WritableSheet;
11 9 import jxl.write.WritableWorkbook;
12 10 import org.apache.commons.lang3.StringUtils;
  11 +import org.apache.poi.ss.usermodel.Row;
13 12 import org.joda.time.DateTime;
14 13 import org.slf4j.Logger;
15 14 import org.slf4j.LoggerFactory;
... ... @@ -40,10 +39,10 @@ public class TTInfoDetailDataToolsImpl implements DataToolsService, TTInfoDetail
40 39 private DataToolsProperties dataToolsProperties;
41 40  
42 41 @Override
43   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  42 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
44 43 try {
45 44 // 对上传的excel文件做处理,将第一个sheet名字设定为工作表1
46   - File file = dataToolsService.uploadFile(filename, filedata);
  45 + DataToolsFile file = dataToolsService.uploadFile(filename, filedata);
47 46 // Workbook workbook = Workbook.getWorkbook(file);
48 47 // Sheet sheet = workbook.getSheet(0);
49 48 //
... ... @@ -73,7 +72,7 @@ public class TTInfoDetailDataToolsImpl implements DataToolsService, TTInfoDetail
73 72 try {
74 73 LOGGER.info("//---------------- 导入时刻表明细 start... ----------------//");
75 74  
76   - String filename = file.getAbsolutePath(); // xls文件名
  75 + String filename = file.getAbsolutePath(); // xls xlsx 文件名
77 76 String sheetname = String.valueOf(params.get("sheetname")); // sheet名字
78 77 Long ttid = Long.valueOf(String.valueOf(params.get("ttid"))); // 时刻表id
79 78 Long xlid = Long.valueOf(String.valueOf(params.get("xlid"))); // 线路id
... ... @@ -89,27 +88,45 @@ public class TTInfoDetailDataToolsImpl implements DataToolsService, TTInfoDetail
89 88 // 1、修改已经上传的excel文件,在每个起点站后标示数字,表示第几个班次
90 89 // 2、由于格式问题,需要把内容都转换成字符串
91 90 List<String> colList = new ArrayList<>();
92   - Workbook workbook = Workbook.getWorkbook(new File(filename));
93   - Sheet sheet = workbook.getSheet(sheetname);
94   - Cell[] cells = sheet.getRow(0);
95   - for (int i = 0; i < cells.length; i++) {
  91 +
  92 + // poi api,并读取第一行数据,组合成站点列表,逗号分隔
  93 + org.apache.poi.ss.usermodel.Workbook poi_workbook;
  94 + org.apache.poi.ss.usermodel.Sheet poi_sheet;
  95 + if (DataToolsFileType.XLS.isThisType(file)) {
  96 + poi_workbook = DataToolsFileType.XLS.getWorkBook(file);
  97 + } else if (DataToolsFileType.XLSX.isThisType(file)) {
  98 + poi_workbook = DataToolsFileType.XLSX.getWorkBook(file);
  99 + } else {
  100 + throw new Exception("不是xls xlsx文件!");
  101 + }
  102 + poi_sheet = poi_workbook.getSheet(sheetname);
  103 + int rownums = poi_sheet.getLastRowNum() + 1;
  104 + int colnums = poi_sheet.getRow(0).getLastCellNum();
  105 + Row firstrow = poi_sheet.getRow(0);
  106 + for (int i = 0; i < colnums; i++) {
  107 + org.apache.poi.ss.usermodel.Cell cell = firstrow.getCell(i);
96 108 if (i == 0) {
97   - colList.add(cells[i].getContents().trim());
  109 + colList.add(PoiUtils.getStringValueFromCell(cell).trim());
98 110 } else {
99   - colList.add(cells[i].getContents() + i);
  111 + colList.add(PoiUtils.getStringValueFromCell(cell) + i);
100 112 }
101 113 }
102 114  
  115 + // jxl api
103 116 File fileCal = new File(filename + "_stringType.xls");
104   - WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal, workbook);
105   - WritableSheet sheet1 = writableWorkbook.getSheet(sheetname);
106   - for (int i = 0; i < sheet1.getColumns(); i++) { // 第一行数据
  117 + WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileCal);
  118 + WritableSheet sheet1 = writableWorkbook.createSheet(sheetname, 0);
  119 + for (int i = 0; i < colnums; i++) { // 第一行数据
107 120 sheet1.addCell(new Label(i, 0, colList.get(i)));
108 121 }
109   - for (int i = 1; i < sheet1.getRows(); i++) { // 第二行开始
110   - Cell[] cells1 = sheet.getRow(i);
111   - for (int j = 0; j < cells1.length; j++) {
112   - sheet1.addCell(new Label(j, i, cells1[j].getContents()));
  122 + for (int i = 1; i < rownums; i++) { // 第二行开始
  123 + for (int j = 0; j < colnums; j++) {
  124 + // poi读
  125 + String cellContent = PoiUtils.getStringValueFromCell(
  126 + poi_sheet.getRow(i).getCell(j)
  127 + );
  128 + // jxl写
  129 + sheet1.addCell(new Label(j, i, cellContent));
113 130 }
114 131 }
115 132 writableWorkbook.write();
... ... @@ -169,7 +186,7 @@ public class TTInfoDetailDataToolsImpl implements DataToolsService, TTInfoDetail
169 186 }
170 187  
171 188 @Override
172   - public File exportData(Map<String, Object> params) throws ScheduleException {
  189 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
173 190 try {
174 191 LOGGER.info("//---------------- 导出时刻表明细 start... ----------------//");
175 192  
... ... @@ -188,7 +205,7 @@ public class TTInfoDetailDataToolsImpl implements DataToolsService, TTInfoDetail
188 205 ktrParms.put("injectktrfile", ktrFile2.getAbsolutePath()); // 注入元数据的ktr文件
189 206 ktrParms.put("ttinfoid", String.valueOf(params.get("ttinfoid")));
190 207  
191   - File file = dataToolsService.exportData(ktrParms);
  208 + DataToolsFile file = dataToolsService.exportData(ktrParms);
192 209  
193 210 LOGGER.info("//---------------- 导出时刻表明细 success... ----------------//");
194 211  
... ...
src/main/java/com/bsth/service/schedule/impl/BServiceImpl.java
... ... @@ -4,6 +4,7 @@ import com.bsth.entity.search.CustomerSpecs;
4 4 import com.bsth.repository.BaseRepository;
5 5 import com.bsth.service.schedule.BService;
6 6 import com.bsth.service.schedule.exception.ScheduleException;
  7 +import com.bsth.service.schedule.utils.DataToolsFile;
7 8 import org.slf4j.Logger;
8 9 import org.slf4j.LoggerFactory;
9 10 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -111,7 +112,7 @@ public class BServiceImpl&lt;T, ID extends Serializable&gt; implements BService&lt;T, ID&gt;
111 112 }
112 113  
113 114 @Override
114   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  115 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
115 116 throw new ScheduleException("子类自己复写此方法!");
116 117 }
117 118  
... ... @@ -121,7 +122,7 @@ public class BServiceImpl&lt;T, ID extends Serializable&gt; implements BService&lt;T, ID&gt;
121 122 }
122 123  
123 124 @Override
124   - public File exportData(Map<String, Object> params) throws ScheduleException {
  125 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
125 126 throw new ScheduleException("子类自己复写此方法!");
126 127 }
127 128 }
... ...
src/main/java/com/bsth/service/schedule/impl/CarConfigInfoServiceImpl.java
... ... @@ -5,6 +5,7 @@ import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
5 5 import com.bsth.service.schedule.CarConfigInfoService;
6 6 import com.bsth.service.schedule.ScheduleRule1FlatService;
7 7 import com.bsth.service.schedule.exception.ScheduleException;
  8 +import com.bsth.service.schedule.utils.DataToolsFile;
8 9 import com.bsth.service.schedule.utils.DataToolsService;
9 10 import org.springframework.beans.factory.annotation.Autowired;
10 11 import org.springframework.beans.factory.annotation.Qualifier;
... ... @@ -30,7 +31,7 @@ public class CarConfigInfoServiceImpl extends BServiceImpl&lt;CarConfigInfo, Long&gt;
30 31 private DataToolsService dataToolsService;
31 32  
32 33 @Override
33   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  34 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
34 35 return dataToolsService.uploadFile(filename, filedata);
35 36 }
36 37  
... ... @@ -40,7 +41,7 @@ public class CarConfigInfoServiceImpl extends BServiceImpl&lt;CarConfigInfo, Long&gt;
40 41 }
41 42  
42 43 @Override
43   - public File exportData(Map<String, Object> params) throws ScheduleException {
  44 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
44 45 return dataToolsService.exportData(params);
45 46 }
46 47  
... ...
src/main/java/com/bsth/service/schedule/impl/CarsServiceImpl.java
... ... @@ -3,6 +3,7 @@ package com.bsth.service.schedule.impl;
3 3 import com.bsth.entity.Cars;
4 4 import com.bsth.service.schedule.CarsService;
5 5 import com.bsth.service.schedule.exception.ScheduleException;
  6 +import com.bsth.service.schedule.utils.DataToolsFile;
6 7 import com.bsth.service.schedule.utils.DataToolsService;
7 8 import org.springframework.beans.factory.annotation.Autowired;
8 9 import org.springframework.beans.factory.annotation.Qualifier;
... ... @@ -29,12 +30,12 @@ public class CarsServiceImpl extends BServiceImpl&lt;Cars, Integer&gt; implements Cars
29 30 }
30 31  
31 32 @Override
32   - public File exportData(Map<String, Object> params) throws ScheduleException {
  33 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
33 34 return dataToolsService.exportData(params);
34 35 }
35 36  
36 37 @Override
37   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  38 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
38 39 return dataToolsService.uploadFile(filename, filedata);
39 40 }
40 41  
... ...
src/main/java/com/bsth/service/schedule/impl/EmployeeConfigInfoServiceImpl.java
... ... @@ -5,6 +5,7 @@ import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
5 5 import com.bsth.service.schedule.EmployeeConfigInfoService;
6 6 import com.bsth.service.schedule.ScheduleRule1FlatService;
7 7 import com.bsth.service.schedule.exception.ScheduleException;
  8 +import com.bsth.service.schedule.utils.DataToolsFile;
8 9 import com.bsth.service.schedule.utils.DataToolsService;
9 10 import org.springframework.beans.factory.annotation.Autowired;
10 11 import org.springframework.beans.factory.annotation.Qualifier;
... ... @@ -36,7 +37,7 @@ public class EmployeeConfigInfoServiceImpl extends BServiceImpl&lt;EmployeeConfigIn
36 37 private JdbcTemplate jdbcTemplate;
37 38  
38 39 @Override
39   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  40 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
40 41 return dataToolsService.uploadFile(filename, filedata);
41 42 }
42 43  
... ... @@ -46,7 +47,7 @@ public class EmployeeConfigInfoServiceImpl extends BServiceImpl&lt;EmployeeConfigIn
46 47 }
47 48  
48 49 @Override
49   - public File exportData(Map<String, Object> params) throws ScheduleException {
  50 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
50 51 return dataToolsService.exportData(params);
51 52 }
52 53  
... ...
src/main/java/com/bsth/service/schedule/impl/EmployeeServiceImpl.java
... ... @@ -3,6 +3,7 @@ package com.bsth.service.schedule.impl;
3 3 import com.bsth.entity.Personnel;
4 4 import com.bsth.service.schedule.EmployeeService;
5 5 import com.bsth.service.schedule.exception.ScheduleException;
  6 +import com.bsth.service.schedule.utils.DataToolsFile;
6 7 import com.bsth.service.schedule.utils.DataToolsService;
7 8 import org.springframework.beans.factory.annotation.Autowired;
8 9 import org.springframework.beans.factory.annotation.Qualifier;
... ... @@ -24,7 +25,7 @@ public class EmployeeServiceImpl extends BServiceImpl&lt;Personnel, Integer&gt; implem
24 25 private DataToolsService dataToolsService;
25 26  
26 27 @Override
27   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  28 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
28 29 return dataToolsService.uploadFile(filename, filedata);
29 30 }
30 31  
... ... @@ -34,7 +35,7 @@ public class EmployeeServiceImpl extends BServiceImpl&lt;Personnel, Integer&gt; implem
34 35 }
35 36  
36 37 @Override
37   - public File exportData(Map<String, Object> params) throws ScheduleException {
  38 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
38 39 return dataToolsService.exportData(params);
39 40 }
40 41  
... ...
src/main/java/com/bsth/service/schedule/impl/GuideboardInfoServiceImpl.java
... ... @@ -5,6 +5,7 @@ import com.bsth.entity.schedule.TTInfoDetail;
5 5 import com.bsth.service.schedule.GuideboardInfoService;
6 6 import com.bsth.service.schedule.TTInfoDetailService;
7 7 import com.bsth.service.schedule.exception.ScheduleException;
  8 +import com.bsth.service.schedule.utils.DataToolsFile;
8 9 import com.bsth.service.schedule.utils.DataToolsService;
9 10 import org.springframework.beans.factory.annotation.Autowired;
10 11 import org.springframework.beans.factory.annotation.Qualifier;
... ... @@ -128,7 +129,7 @@ public class GuideboardInfoServiceImpl extends BServiceImpl&lt;GuideboardInfo, Long
128 129 }
129 130  
130 131 @Override
131   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  132 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
132 133 return dataToolsService.uploadFile(filename, filedata);
133 134 }
134 135  
... ... @@ -138,7 +139,7 @@ public class GuideboardInfoServiceImpl extends BServiceImpl&lt;GuideboardInfo, Long
138 139 }
139 140  
140 141 @Override
141   - public File exportData(Map<String, Object> params) throws ScheduleException {
  142 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
142 143 return dataToolsService.exportData(params);
143 144 }
144 145  
... ...
src/main/java/com/bsth/service/schedule/impl/ScheduleRule1FlatServiceImpl.java
... ... @@ -3,6 +3,7 @@ package com.bsth.service.schedule.impl;
3 3 import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
4 4 import com.bsth.service.schedule.ScheduleRule1FlatService;
5 5 import com.bsth.service.schedule.exception.ScheduleException;
  6 +import com.bsth.service.schedule.utils.DataToolsFile;
6 7 import com.bsth.service.schedule.utils.DataToolsService;
7 8 import org.springframework.beans.factory.annotation.Autowired;
8 9 import org.springframework.beans.factory.annotation.Qualifier;
... ... @@ -21,7 +22,7 @@ public class ScheduleRule1FlatServiceImpl extends BServiceImpl&lt;ScheduleRule1Flat
21 22 private DataToolsService dataToolsService;
22 23  
23 24 @Override
24   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  25 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
25 26 return dataToolsService.uploadFile(filename, filedata);
26 27 }
27 28  
... ... @@ -31,7 +32,7 @@ public class ScheduleRule1FlatServiceImpl extends BServiceImpl&lt;ScheduleRule1Flat
31 32 }
32 33  
33 34 @Override
34   - public File exportData(Map<String, Object> params) throws ScheduleException {
  35 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
35 36 return dataToolsService.exportData(params);
36 37 }
37 38 }
... ...
src/main/java/com/bsth/service/schedule/impl/TTInfoDetailServiceImpl.java
... ... @@ -23,30 +23,29 @@ import com.bsth.service.schedule.GuideboardInfoService;
23 23 import com.bsth.service.schedule.TTInfoDetailService;
24 24 import com.bsth.service.schedule.datatools.TTInfoDetailForEdit;
25 25 import com.bsth.service.schedule.exception.ScheduleException;
26   -import com.bsth.service.schedule.utils.DataToolsProperties;
  26 +import com.bsth.service.schedule.utils.DataToolsFile;
  27 +import com.bsth.service.schedule.utils.DataToolsFileType;
27 28 import com.bsth.service.schedule.utils.DataToolsService;
28   -import jxl.Cell;
29   -import jxl.Sheet;
30   -import jxl.Workbook;
31   -import jxl.write.Label;
32   -import jxl.write.WritableSheet;
33   -import jxl.write.WritableWorkbook;
  29 +import com.bsth.service.schedule.utils.PoiUtils;
34 30 import org.apache.commons.lang3.StringUtils;
35   -import org.joda.time.DateTime;
  31 +import org.apache.poi.ss.usermodel.Cell;
  32 +import org.apache.poi.ss.usermodel.Row;
  33 +import org.apache.poi.ss.usermodel.Sheet;
  34 +import org.apache.poi.ss.usermodel.Workbook;
36 35 import org.slf4j.Logger;
37 36 import org.slf4j.LoggerFactory;
38 37 import org.springframework.beans.factory.annotation.Autowired;
39 38 import org.springframework.beans.factory.annotation.Qualifier;
40   -import org.springframework.boot.context.properties.EnableConfigurationProperties;
41 39 import org.springframework.jdbc.core.JdbcTemplate;
42 40 import org.springframework.stereotype.Service;
43 41 import org.springframework.transaction.annotation.Transactional;
44 42 import org.springframework.util.CollectionUtils;
45 43  
46 44 import java.io.File;
47   -import java.io.PrintWriter;
48   -import java.io.StringWriter;
49   -import java.util.*;
  45 +import java.util.ArrayList;
  46 +import java.util.HashMap;
  47 +import java.util.List;
  48 +import java.util.Map;
50 49 import java.util.regex.Matcher;
51 50 import java.util.regex.Pattern;
52 51  
... ... @@ -122,7 +121,7 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
122 121 }
123 122  
124 123 @Override
125   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  124 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
126 125 return dataToolsService.uploadFile(filename, filedata);
127 126 }
128 127  
... ... @@ -134,7 +133,7 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
134 133 }
135 134  
136 135 @Override
137   - public File exportData(Map<String, Object> params) throws ScheduleException {
  136 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
138 137 return dataToolsService.exportData(params);
139 138 }
140 139  
... ... @@ -152,17 +151,30 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
152 151 @Override
153 152 public void validateExcelSheet(String filename, String sheetname, Integer lineid, String linename) throws ScheduleException {
154 153 try {
155   - Workbook book = Workbook.getWorkbook(new File(filename));
156   - Sheet sheet = book.getSheet(sheetname);
157   - if (sheet.getRows() == 0 || sheet.getColumns() == 0) { // 工作区是否为空
  154 + File file = new File(filename);
  155 + Workbook workbook;
  156 +
  157 + if (DataToolsFileType.XLS.isThisType(file)) {
  158 + workbook = DataToolsFileType.XLS.getWorkBook(file);
  159 + } else if (DataToolsFileType.XLSX.isThisType(file)) {
  160 + workbook = DataToolsFileType.XLSX.getWorkBook(file);
  161 + } else {
  162 + throw new Exception("不是xls xlsx文件!");
  163 + }
  164 +
  165 + Sheet sheet = workbook.getSheet(sheetname);
  166 + int rowNums = sheet.getLastRowNum() + 1; // 基于0 base的,长度加1
  167 + int colNums = sheet.getRow(0).getLastCellNum(); // 不需要加1,就是长度
  168 +
  169 + if (rowNums == 0 || colNums == 0) { // 工作区是否为空
158 170 throw new Exception(String.format("%s 工作区没有数据!", sheetname));
159 171 } else {
160   - if (sheet.getRows() <= 1 || sheet.getColumns() <= 1) {
  172 + if (rowNums <= 1 || rowNums <= 1) {
161 173 throw new Exception(String.format("工作区至少包含2行2列的数据"));
162 174 } else {
163   - Cell[] cells = sheet.getRow(0); // 获取第一行数据列
164   - for (int i = 0; i < cells.length; i++) {
165   - String cell_con = StringUtils.trimToEmpty(cells[i].getContents()); // trimToEmpty
  175 + Row firstRow = sheet.getRow(0); // 获取第一行数据列
  176 + for (int i = 0; i < colNums; i++) {
  177 + String cell_con = StringUtils.trimToEmpty(PoiUtils.getStringValueFromCell(firstRow.getCell(i))); // trimToEmpty
166 178  
167 179 if (StringUtils.isEmpty(cell_con)) {
168 180 throw new Exception(String.format("第1行,第%d列数据不能为空", i + 1));
... ... @@ -203,9 +215,9 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
203 215  
204 216 // 验证路牌内容
205 217 Map<String, Integer> gbindexmap = new HashMap<>(); // 记录每个路牌在第几行
206   - for (int i = 1; i < sheet.getRows(); i++) { // 从第2行开始验证数据
207   - Cell bcell = sheet.getRow(i)[0]; // 获取第1列
208   - String bcell_con = StringUtils.trimToEmpty(bcell.getContents()); // trimToEmpty
  218 + for (int i = 1; i < rowNums; i++) { // 从第2行开始验证数据
  219 + Cell cell = sheet.getRow(i).getCell(0); // 获取第1列
  220 + String bcell_con = StringUtils.trimToEmpty(PoiUtils.getStringValueFromCell(cell)); // trimToEmpty
209 221 if (StringUtils.isEmpty(bcell_con)) {
210 222 throw new Exception(String.format("第%d行,第1列路牌无数据", i + 1));
211 223 } else if (gbindexmap.get(bcell_con.trim()) != null) {
... ... @@ -236,10 +248,10 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
236 248 Pattern p2 = Pattern.compile(rex2);
237 249 Pattern p3 = Pattern.compile(rex3);
238 250  
239   - for (int i = 1; i < sheet.getRows(); i++) { // 从第2行开始验证数据
240   - Cell[] bcells = sheet.getRow(i);
241   - for (int j = 1; j < bcells.length; j++) { // 从第2列开始
242   - String bcell_con = StringUtils.trimToEmpty(bcells[j].getContents()); // trimToEmpty
  251 + for (int i = 1; i < rowNums; i++) { // 从第2行开始验证数据
  252 + Row row = sheet.getRow(i);
  253 + for (int j = 1; j < colNums; j++) { // 从第2列开始
  254 + String bcell_con = StringUtils.trimToEmpty(PoiUtils.getStringValueFromCell(row.getCell(j))); // trimToEmpty
243 255 if (StringUtils.isNotEmpty(bcell_con)) {
244 256 Matcher m1 = p1.matcher(bcell_con.trim());
245 257 Matcher m2 = p2.matcher(bcell_con.trim());
... ... @@ -253,6 +265,7 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
253 265 }
254 266  
255 267 }
  268 +
256 269 } catch (Exception exp) {
257 270 exp.printStackTrace();
258 271 throw new ScheduleException(exp.getMessage());
... ... @@ -315,13 +328,13 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
315 328 }
316 329  
317 330 /**
318   - * @description : (TODO) 时刻表明细模型数据保存.
  331 + * @description (TODO) 时刻表明细模型数据保存.
319 332 *
320   - * @param : [map]
  333 + * @param map
321 334 *
322 335 * @return : 返回保存操作后的状态.
323 336 *
324   - * @exception : 处理所有抛出来的异常.
  337 + * @exception 处理所有抛出来的异常.
325 338 * */
326 339 @Transactional
327 340 public Map<String, Object> skbDetailMxSave(Map<String, Object> map) {
... ... @@ -347,11 +360,11 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
347 360 /**
348 361 * @description : (TODO) json班次数据转list班次.
349 362 *
350   - * @param : [jsonStr--班次json字符串]
  363 + * @param jsonStr 班次json字符串]
351 364 *
352   - * @return :返回一个list分装的班次数据.
  365 + * @return 返回一个list分装的班次数据.
353 366 *
354   - * @status : OK.
  367 + * @status OK.
355 368 * */
356 369 public List<TTInfoDetail> jsonArrayToListEntity(String jsonStr) throws Exception {
357 370 // 1、创建list分装的时刻表明细实体对象数据.
... ... @@ -369,9 +382,9 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
369 382 /**
370 383 * @description : (TODO) 班次map对象转实体对象.
371 384 *
372   - * @param : [obj-班次map对象].
  385 + * @param obj 班次map对象.
373 386 *
374   - * @return : 返回一个班次实体对象.
  387 + * @return 返回一个班次实体对象.
375 388 *
376 389 * @exception 异常暂先抛出去.
377 390 * */
... ... @@ -427,7 +440,7 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
427 440 /**
428 441 * @description : (TODO) int转boolean类型.
429 442 *
430   - * @param : [value--int类型的数值]
  443 + * @param value--int类型的数值]
431 444 *
432 445 * @return : 返回一个布尔类型值.
433 446 * */
... ... @@ -441,9 +454,9 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
441 454 }
442 455  
443 456 /**
444   - * @description : (TODO) 获取路牌.
  457 + * @description (TODO) 获取路牌.
445 458 *
446   - * @param [xl--线路,name--路牌名称,code--路牌编码,lpType--路牌类型]
  459 + * @param xl --线路,name--路牌名称,code--路牌编码,lpType--路牌类型]
447 460 *
448 461 * @return 返回路牌.
449 462 * */
... ... @@ -479,11 +492,11 @@ public class TTInfoDetailServiceImpl extends BServiceImpl&lt;TTInfoDetail, Long&gt; im
479 492 /**
480 493 * @description : (TODO) 线路方向转代码.
481 494 *
482   - * @param : [str--方向字符串]
  495 + * @param str--方向字符串]
483 496 *
484 497 * @return 返回方向代码.
485 498 *
486   - * @exception : 异常暂先抛出.
  499 + * @exception 异常暂先抛出.
487 500 * */
488 501 public String dirToCod(String str) throws Exception {
489 502 String c = "";
... ...
src/main/java/com/bsth/service/schedule/utils/DataToolsFile.java 0 → 100644
  1 +package com.bsth.service.schedule.utils;
  2 +
  3 +import java.io.File;
  4 +
  5 +/**
  6 + * 数据工具文件(操作后的)。
  7 + */
  8 +public class DataToolsFile {
  9 + /** 文件类型 */
  10 + private DataToolsFileType fileType;
  11 + /** 具体文件 */
  12 + private File file;
  13 +
  14 + public DataToolsFileType getFileType() {
  15 + return fileType;
  16 + }
  17 +
  18 + public void setFileType(DataToolsFileType fileType) {
  19 + this.fileType = fileType;
  20 + }
  21 +
  22 + public File getFile() {
  23 + return file;
  24 + }
  25 +
  26 + public void setFile(File file) {
  27 + this.file = file;
  28 + }
  29 +}
... ...
src/main/java/com/bsth/service/schedule/utils/DataToolsFileType.java 0 → 100644
  1 +package com.bsth.service.schedule.utils;
  2 +
  3 +import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  4 +import org.apache.poi.ss.usermodel.Workbook;
  5 +import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  6 +import org.apache.tika.Tika;
  7 +
  8 +import java.io.File;
  9 +import java.io.FileInputStream;
  10 +
  11 +/**
  12 + * 数据工具文件类型。
  13 + */
  14 +public enum DataToolsFileType {
  15 + XLS { // excel xls 文件
  16 + @Override
  17 + public boolean isThisType(byte[] fileData) {
  18 + Tika tika = new Tika();
  19 + String type = tika.detect(fileData);
  20 +
  21 + // application/x-tika-msoffice
  22 + if ("application/vnd.ms-excel".equals(type) ||
  23 + "application/x-tika-msoffice".equals(type)) {
  24 + // .xls 2007的格式
  25 + return true;
  26 + } else {
  27 + return false;
  28 + }
  29 + }
  30 +
  31 + @Override
  32 + public boolean isThisType(File file) {
  33 + try {
  34 + Tika tika = new Tika();
  35 + String type = tika.detect(file);
  36 +
  37 + // application/x-tika-msoffice
  38 + if ("application/vnd.ms-excel".equals(type) ||
  39 + "application/x-tika-msoffice".equals(type)) {
  40 + // .xls 2007的格式
  41 + return true;
  42 + } else {
  43 + return false;
  44 + }
  45 +
  46 + } catch (Exception exp) {
  47 + throw new RuntimeException(exp);
  48 + }
  49 +
  50 + }
  51 +
  52 + @Override
  53 + public String getNewFileName(String... fileNames) {
  54 + StringBuilder stringBuilder = new StringBuilder();
  55 + for (String fn: fileNames) {
  56 + stringBuilder.append(fn);
  57 + }
  58 + stringBuilder.append(".xls");
  59 + return stringBuilder.toString();
  60 + }
  61 +
  62 + @Override
  63 + public Workbook getWorkBook(File file) {
  64 + try {
  65 + return new HSSFWorkbook(new FileInputStream(file));
  66 + } catch (Exception exp) {
  67 + throw new RuntimeException(exp);
  68 + }
  69 + }
  70 + },
  71 + XLSX { // excel xlsx 文件
  72 + @Override
  73 + public boolean isThisType(byte[] fileData) {
  74 + Tika tika = new Tika();
  75 + String type = tika.detect(fileData);
  76 +
  77 + // application/x-tika-ooxml
  78 + if ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(type) ||
  79 + "application/x-tika-ooxml".equals(type)) {
  80 + // .xlsx 2007之后的格式
  81 + return true;
  82 + } else {
  83 + return false;
  84 + }
  85 +
  86 + }
  87 +
  88 + @Override
  89 + public boolean isThisType(File file) {
  90 + try {
  91 + Tika tika = new Tika();
  92 + String type = tika.detect(file);
  93 +
  94 + // application/x-tika-ooxml
  95 + if ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(type) ||
  96 + "application/x-tika-ooxml".equals(type)) {
  97 + // .xlsx 2007之后的格式
  98 + return true;
  99 + } else {
  100 + return false;
  101 + }
  102 +
  103 + } catch (Exception exp) {
  104 + throw new RuntimeException(exp);
  105 + }
  106 + }
  107 +
  108 + @Override
  109 + public String getNewFileName(String... fileNames) {
  110 + StringBuilder stringBuilder = new StringBuilder();
  111 + for (String fn: fileNames) {
  112 + stringBuilder.append(fn);
  113 + }
  114 + stringBuilder.append(".xlsx");
  115 + return stringBuilder.toString();
  116 + }
  117 +
  118 + @Override
  119 + public Workbook getWorkBook(File file) {
  120 + try {
  121 + return new XSSFWorkbook(file);
  122 + } catch (Exception exp) {
  123 + throw new RuntimeException(exp);
  124 + }
  125 +
  126 + }
  127 + };
  128 +
  129 + /**
  130 + * 判定是否是此文件类型。
  131 + * @param fileData
  132 + * @return
  133 + */
  134 + public abstract boolean isThisType(byte[] fileData);
  135 +
  136 + /**
  137 + * 判定文件类型。
  138 + * @param file
  139 + * @return
  140 + */
  141 + public abstract boolean isThisType(File file);
  142 +
  143 + /**
  144 + * 生成对应的文件名
  145 + * @param fileNames 文件名组成部份
  146 + * @return
  147 + */
  148 + public abstract String getNewFileName(String ... fileNames);
  149 +
  150 + /**
  151 + * 获取Excel工作本对象。
  152 + * @param file 文件
  153 + * @return
  154 + */
  155 + public abstract Workbook getWorkBook(File file);
  156 +}
... ...
src/main/java/com/bsth/service/schedule/utils/DataToolsService.java
... ... @@ -11,9 +11,9 @@ import java.util.Map;
11 11 public interface DataToolsService {
12 12 //----------------- 数据服务操作 --------------//
13 13 // 上传文件
14   - File uploadFile(String filename, byte[] filedata) throws ScheduleException;
  14 + DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException;
15 15 // 导入数据
16 16 void importData(File file, Map<String, Object> params) throws ScheduleException;
17 17 // 导出数据
18   - File exportData(Map<String, Object> params) throws ScheduleException;
  18 + DataToolsFile exportData(Map<String, Object> params) throws ScheduleException;
19 19 }
... ...
src/main/java/com/bsth/service/schedule/utils/DataToolsServiceImpl.java
... ... @@ -2,7 +2,6 @@ package com.bsth.service.schedule.utils;
2 2  
3 3 import com.bsth.service.schedule.exception.ScheduleException;
4 4 import com.google.common.io.Files;
5   -import org.apache.tika.Tika;
6 5 import org.joda.time.DateTime;
7 6 import org.pentaho.di.core.KettleEnvironment;
8 7 import org.pentaho.di.core.logging.KettleLogStore;
... ... @@ -91,33 +90,46 @@ public class DataToolsServiceImpl implements DataToolsService {
91 90 }
92 91  
93 92 @Override
94   - public File uploadFile(String filename, byte[] filedata) throws ScheduleException {
  93 + public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
95 94 // 上传文件
96 95 try {
97 96 initKettle();
98 97  
99 98 LOGGER.info("start uploadFile...originalFilename={}", filename);
100   - File newFile = new File(dataToolsProperties.getFileuploadDir() + File.separator +
101   - filename + "-upload-" + new DateTime().toString("yyyyMMddHHmmss") + ".xls");
102   - // TODO:判定是否excel数据
103   - Tika tika = new Tika();
104   - String type = tika.detect(filedata);
105   - // application/x-tika-msoffice
106   - LOGGER.info("文件格式={}", type);
107   - if ("application/vnd.ms-excel".equals(type) || "application/x-tika-msoffice".equals(type)) {
108   - // .xls 2007的格式
109   - Files.write(filedata, newFile);
110   - } else if ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(type)) {
111   - // .xlsx 2007之后的格式
112   - throw new Exception("暂时不支持.xlsx格式文件!");
  99 +
  100 + DataToolsFile dataToolsFile = new DataToolsFile();
  101 +
  102 + // 判定文件类型(目前只支持xls,xlsx文件)
  103 + if (DataToolsFileType.XLS.isThisType(filedata)) {
  104 + // xls文件
  105 + dataToolsFile.setFileType(DataToolsFileType.XLS);
  106 + dataToolsFile.setFile(new File(DataToolsFileType.XLS.getNewFileName(
  107 + dataToolsProperties.getFileuploadDir(),
  108 + File.separator,
  109 + filename,
  110 + "-upload-",
  111 + new DateTime().toString("yyyyMMddHHmmss")
  112 + )));
  113 + Files.write(filedata, dataToolsFile.getFile());
  114 + } else if (DataToolsFileType.XLSX.isThisType(filedata)) {
  115 + // xlsx文件
  116 + dataToolsFile.setFileType(DataToolsFileType.XLSX);
  117 + dataToolsFile.setFile(new File(DataToolsFileType.XLSX.getNewFileName(
  118 + dataToolsProperties.getFileuploadDir(),
  119 + File.separator,
  120 + filename,
  121 + "-upload-",
  122 + new DateTime().toString("yyyyMMddHHmmss")
  123 + )));
  124 + Files.write(filedata, dataToolsFile.getFile());
113 125 } else {
114 126 // 非excel文件
115   - throw new Exception("非.xls格式文件!");
  127 + throw new Exception("非.xls .xlsx 格式文件!");
116 128 }
117 129  
118   - LOGGER.info("uploadFile success...newFilename={}", newFile.getAbsolutePath());
  130 + LOGGER.info("uploadFile success...newFilename={}", dataToolsFile.getFile().getAbsolutePath());
119 131  
120   - return newFile;
  132 + return dataToolsFile;
121 133 } catch (Exception exp) {
122 134 LOGGER.info("uploadFile failed...stackTrace...");
123 135  
... ... @@ -194,7 +206,7 @@ public class DataToolsServiceImpl implements DataToolsService {
194 206 }
195 207  
196 208 @Override
197   - public File exportData(Map<String, Object> params) throws ScheduleException {
  209 + public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
198 210 // 导出数据
199 211 String transLogId = "";
200 212 String transMetaLogId = "";
... ... @@ -251,7 +263,12 @@ public class DataToolsServiceImpl implements DataToolsService {
251 263 LOGGER.info(stringBuffer.toString());
252 264 LOGGER.info("exportData success...");
253 265  
254   - return new File(filepath + ".xls");
  266 + // 导出目前是xls格式
  267 + DataToolsFile dataToolsFile = new DataToolsFile();
  268 + dataToolsFile.setFileType(DataToolsFileType.XLS);
  269 + dataToolsFile.setFile(new File(filepath + ".xls"));
  270 +
  271 + return dataToolsFile;
255 272 } catch (Exception exp) {
256 273 LOGGER.info("exportData failed...statckTrace...");
257 274  
... ...
src/main/java/com/bsth/service/schedule/utils/PoiUtils.java 0 → 100644
  1 +package com.bsth.service.schedule.utils;
  2 +
  3 +import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  4 +import org.apache.poi.ss.usermodel.Cell;
  5 +import org.apache.poi.xssf.usermodel.XSSFCell;
  6 +
  7 +import java.text.DecimalFormat;
  8 +import java.text.SimpleDateFormat;
  9 +import java.util.Date;
  10 +
  11 +/**
  12 + * POI操作的until方法。
  13 + */
  14 +public class PoiUtils {
  15 + /**
  16 + * 使用poi读取cell中的值
  17 + * @param cell
  18 + * @return
  19 + */
  20 + public static String getStringValueFromCell(Cell cell) {
  21 + SimpleDateFormat sFormat = new SimpleDateFormat("MM/dd/yyyy");
  22 + DecimalFormat decimalFormat = new DecimalFormat("#.#");
  23 + String cellValue = "";
  24 + if(cell == null) {
  25 + return cellValue;
  26 + }
  27 + else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
  28 + cellValue = cell.getStringCellValue();
  29 + }
  30 +
  31 + else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
  32 + if(HSSFDateUtil.isCellDateFormatted(cell)) {
  33 + double d = cell.getNumericCellValue();
  34 + Date date = HSSFDateUtil.getJavaDate(d);
  35 + cellValue = sFormat.format(date);
  36 + }
  37 + else {
  38 + cellValue = decimalFormat.format((cell.getNumericCellValue()));
  39 + }
  40 + }
  41 + else if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
  42 + cellValue = "";
  43 + }
  44 + else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
  45 + cellValue = String.valueOf(cell.getBooleanCellValue());
  46 + }
  47 + else if(cell.getCellType() == Cell.CELL_TYPE_ERROR) {
  48 + cellValue = "";
  49 + }
  50 + else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
  51 + cellValue = cell.getCellFormula().toString();
  52 + }
  53 + return cellValue;
  54 + }
  55 +}
... ...