poi 导出excel 只需要模板的设置就可以输出想要的格式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34359363/article/details/53333261
                  poi 导出excel 
用poi导出excel,相信大家已经有了很多的文档和资料可以去参考。但是如果有excel模板,我们怎么通过对模板的设置,得到我们想要的excek格式?怎么有一个可以通用的方法去实现那?
                                                                                   重点来了
	XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fi));// 按照模板创建一个excel文件
				XSSFSheet sheet = workbook.getSheetAt(0);// 得到模板中的sheet
				XSSFRow row = sheet.getRow((short) 0);
				XSSFRow rowTitle = sheet.getRow((short) 0);
				int row1 = sheet.getPhysicalNumberOfRows();// 得到模板的sheet中的最后一行
				System.out.println("最后的一行" + row1);
				XSSFCell cell;
				XSSFCell cellTitle = null;
				ResultSetMetaData md = rs.getMetaData();// 得到resultset中的结果集
				int nColumn = md.getColumnCount(); // 得到查出来的列数
				System.out.println(nColumn);
				int iRow = row1;// 作用是从第几行开始到导入
				  // 设置字体
		        XSSFFont font = workbook.createFont();
		        font.setFontHeightInPoints((short) 9); //字体高度
		        font.setColor(XSSFFont.DEFAULT_FONT_COLOR); //字体颜色
	            font.setFontName("Arial"); //字体
	            XSSFCellStyle cellStyle = workbook.createCellStyle();// 设置单元格
	            Map<Integer, CellStyle> map = new HashMap<Integer, CellStyle>();
				for (int j = 1; j <= nColumn; j++) {
					// 得到表头的样式
					cellTitle = rowTitle.getCell((short) (j - 1));
					// System.out.println("Back>>>"+cellTitle.getCellStyle().getFillBackgroundColorColor().getIndexed());
					// System.out.println("Fore>>>"+cellTitle.getCellStyle().getFillForegroundColorColor().getIndexed());
					// cell = row.createCell((short) (j - 1));
					cellStyle = null;
					
					cellStyle = (XSSFCellStyle) cellTitle.getCellStyle().clone();
					cellStyle.setFont(font);
					// 设置背景色
					cellStyle.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
					// 设置前景色
					cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
					map.put(j, cellStyle);
				}

				while (rs.next()) {
					row = sheet.createRow((short) iRow);
					row.setHeight((short) 1000);
					for (int j = 1; j <= nColumn; j++) {
						cell = row.createCell((short) (j - 1));
						cell.setCellStyle(map.get(j));
						if (rs.getObject(j) != null){
							if(rs.getMetaData().getColumnTypeName(j).equals("DATE")){
								cell.setCellValue(rs.getDate(j));
							}else if(rs.getMetaData().getColumnTypeName(j).equals("NUMBER")){
								cell.setCellValue(rs.getDouble(j));
							}else{
								//亚马逊报表
								
							    if(rs.getMetaData().getColumnName(j).toUpperCase().equals("CARGO_VALUE") && !rs.getObject(j).equals("OBC")){
									cell.setCellValue(rs.getDouble(j));
								}else if(rs.getMetaData().getColumnName(j).toUpperCase().equals("PICKUP_DATE") && !rs.getObject(j).equals("SPU")){
									cell.setCellValue(sdf3.parse(rs.getObject(j).toString()));
								}else if(rs.getMetaData().getColumnName(j).toUpperCase().equals("EXPORT_DECLARATION") && !rs.getObject(j).equals("OBC")){
									cell.setCellValue(sdf3.parse(rs.getObject(j).toString()));
								}else if(rs.getMetaData().getColumnName(j).toUpperCase().equals("PREIMPORT_DECLARATION") && !rs.getObject(j).equals("OBD")){
									cell.setCellValue(sdf3.parse(rs.getObject(j).toString()));
								}else{
									cell.setCellValue(rs.getObject(j).toString());
								}
							}
                        //sheet.autoSizeColumn((short)j);//自动调整行宽
						}else{
							cell.setCellValue("");
						}
					}
					iRow++;
				}
//				System.out.println(cellTitle.getCellStyle().getFillBackgroundColorColor().getIndexed());
//				System.out.println(cellTitle.getCellStyle().getFillForegroundColorColor().getIndexed());
				xlsName = URLDecoder.decode(xlsName, "utf-8");
				File file = new File(realpathx1);
				if (file.exists()) {
					FileOutputStream fOut = new FileOutputStream(realpath1);
					workbook.write(fOut);
					fOut.flush();
					fOut.close();
				} else {
					file.mkdirs();
					FileOutputStream fOut = new FileOutputStream(realpath1);
					workbook.write(fOut);
					fOut.flush();
					fOut.close();
				}

			}

 代码贴上去了 希望对poi到excel业务的 小伙伴有用

猜你喜欢

转载自blog.csdn.net/qq_34359363/article/details/53333261