Commit 5ac8ee76d37ef6bffefaee2e2f833e06a0660caf
1 parent
ce0a43c01a
Exists in
master
and in
6 other branches
excl
Showing 1 changed file with 96 additions and 8 deletions
platform-common/src/main/java/com/lyms/platform/common/utils/ExcelUtil.java
View file @
5ac8ee7
| ... | ... | @@ -9,15 +9,13 @@ |
| 9 | 9 | import jxl.Cell; |
| 10 | 10 | import jxl.Sheet; |
| 11 | 11 | import jxl.Workbook; |
| 12 | +import jxl.format.*; | |
| 13 | +import jxl.format.Alignment; | |
| 12 | 14 | import jxl.format.Colour; |
| 13 | -import jxl.format.UnderlineStyle; | |
| 15 | +import jxl.format.VerticalAlignment; | |
| 14 | 16 | import jxl.read.biff.BiffException; |
| 15 | -import jxl.write.Label; | |
| 16 | -import jxl.write.WritableCellFormat; | |
| 17 | -import jxl.write.WritableFont; | |
| 18 | -import jxl.write.WritableSheet; | |
| 19 | -import jxl.write.WritableWorkbook; | |
| 20 | -import jxl.write.WriteException; | |
| 17 | +import jxl.write.*; | |
| 18 | +import jxl.write.Number; | |
| 21 | 19 | import jxl.write.biff.RowsExceededException; |
| 22 | 20 | |
| 23 | 21 | /** |
| 24 | 22 | |
| ... | ... | @@ -237,8 +235,98 @@ |
| 237 | 235 | |
| 238 | 236 | } |
| 239 | 237 | |
| 238 | + | |
| 239 | + public static void readExclFile(String filePath) | |
| 240 | + { | |
| 241 | + File file = new File(filePath); | |
| 242 | + Workbook wb = null; | |
| 243 | + try { | |
| 244 | + wb = Workbook.getWorkbook(file); | |
| 245 | + WritableWorkbook book = wb.createWorkbook(file, wb); | |
| 246 | + WritableSheet ws = book.getSheet(0); | |
| 247 | + | |
| 248 | + List<Integer> columNames = new ArrayList<>(); | |
| 249 | + | |
| 250 | + for (int i = 0, len = 37; i < len; i++) | |
| 251 | + { | |
| 252 | + columNames.add(i); | |
| 253 | + } | |
| 254 | + | |
| 255 | + WritableFont contentFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); | |
| 256 | + WritableCellFormat contentFormt = new WritableCellFormat(contentFont); | |
| 257 | + contentFormt.setAlignment(jxl.format.Alignment.CENTRE); | |
| 258 | + contentFormt.setVerticalAlignment(VerticalAlignment.CENTRE); | |
| 259 | + | |
| 260 | + WritableFont leftFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); | |
| 261 | + WritableCellFormat leftFormt = new WritableCellFormat(leftFont); | |
| 262 | + leftFormt.setAlignment(Alignment.LEFT); | |
| 263 | + leftFormt.setVerticalAlignment(VerticalAlignment.CENTRE); | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + WritableFont title = new WritableFont(WritableFont.ARIAL, 18, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); | |
| 270 | + WritableCellFormat titleFormt = new WritableCellFormat(title); | |
| 271 | + titleFormt.setAlignment(jxl.format.Alignment.CENTRE); | |
| 272 | + titleFormt.setVerticalAlignment(VerticalAlignment.CENTRE); | |
| 273 | + | |
| 274 | + for (int i = 0; i < ws.getRows() ; i++) { | |
| 275 | + for (int j = 0; j < ws.getColumns(); j++) { | |
| 276 | + if(i == 0 && j == 0){ | |
| 277 | + Label label = new Label(j, i, "四川省成都市统计",titleFormt); | |
| 278 | + ws.addCell(label); | |
| 279 | + } | |
| 280 | + | |
| 281 | + if(i == 1 && j == 0){ | |
| 282 | + Label label = new Label(j, i, "成都市",leftFormt); | |
| 283 | + ws.addCell(label); | |
| 284 | + } | |
| 285 | + | |
| 286 | + if(i == 1 && j == 19){ | |
| 287 | + Label label = new Label(j, i, "2018-08-20",leftFormt); | |
| 288 | + ws.addCell(label); | |
| 289 | + } | |
| 290 | + } | |
| 291 | + } | |
| 292 | + | |
| 293 | + | |
| 294 | + for (int i = 5, len = 30; i < len; i++) | |
| 295 | + { | |
| 296 | + for (int j = 0; j < columNames.size() ; j++) | |
| 297 | + { | |
| 298 | + WritableCellFormat wcfN = new WritableCellFormat(contentFont); | |
| 299 | + wcfN.setAlignment(jxl.format.Alignment.CENTRE); | |
| 300 | + wcfN.setVerticalAlignment(VerticalAlignment.CENTRE); | |
| 301 | + Number labelNF = new Number(j, i,j, wcfN); | |
| 302 | + ws.addCell(labelNF); | |
| 303 | + } | |
| 304 | + | |
| 305 | + } | |
| 306 | + | |
| 307 | + ws.addCell(new Label(0, 30, "注:统计时限:上月26日至本月25日;分娩方式中其他包括:吸引产、臀助产、臀牵引、产钳产等。", leftFormt)); | |
| 308 | + | |
| 309 | + ws.mergeCells(0, 30, 36, 30); | |
| 310 | + | |
| 311 | + ws.addCell(new Label(0, 31, "填报单位:", leftFormt)); | |
| 312 | + ws.mergeCells(0, 31, 5, 31); | |
| 313 | + ws.addCell(new Label(6, 31, "填报人:", leftFormt)); | |
| 314 | + ws.mergeCells(6, 31, 10, 31); | |
| 315 | + ws.addCell(new Label(11, 31, "填报日期:", leftFormt)); | |
| 316 | + ws.mergeCells(11, 31,36, 31); | |
| 317 | + | |
| 318 | + book.write(); | |
| 319 | + book.close(); | |
| 320 | + wb.close(); | |
| 321 | + }catch (Exception e) | |
| 322 | + { | |
| 323 | + e.printStackTrace(); | |
| 324 | + } | |
| 325 | + | |
| 326 | + } | |
| 327 | + | |
| 240 | 328 | public static void main(String[] args) { |
| 241 | - ExcelUtil.readExcl("F:\\1.xls"); | |
| 329 | + ExcelUtil.readExclFile("F:\\需求文档\\my.xls"); | |
| 242 | 330 | } |
| 243 | 331 | |
| 244 | 332 | } |