dataMap = datalist.get(i);
+ for (int j = 0; j < tempCells.size(); j++) {
+ TempCell tempCell = tempCells.get(j);
+ tempCell.setRow(tempCell.getRow() + 1);
+ tempCell.setData(dataMap.get(j + 1));
+ setCell(tempCell, wsheet);
+ }
+ }
+
+
+ }
+
+
+ /**
+ *
+ * 功能:获取输入工作区
+ *
+ *
+ * @param tempFilePath
+ * @return
+ * @throws FileNotFoundException
+ * @throws IOException
+ * @date 2015年9月26日
+ * @author zq
+ */
+ private Workbook getTempWorkbook(String tempFilePath) throws FileNotFoundException, IOException {
+ if (!tempWorkbook.containsKey(tempFilePath)) {
+ if (tempFilePath.endsWith(".xlsx")) {
+ tempWorkbook.put(tempFilePath, new XSSFWorkbook(getFileInputStream(tempFilePath)));
+ } else if (tempFilePath.endsWith(".xls")) {
+ tempWorkbook.put(tempFilePath, new HSSFWorkbook(getFileInputStream(tempFilePath)));
+ }
+ }
+ return tempWorkbook.get(tempFilePath);
+ }
+
+ /**
+ *
+ * 功能:获得模板输入流
+ *
+ *
+ * @param tempFilePath
+ * @return
+ * @throws FileNotFoundException
+ * @date 2015年9月26日
+ * @author zq
+ */
+ private FileInputStream getFileInputStream(String tempFilePath) throws FileNotFoundException {
+ if (!tempStream.containsKey(tempFilePath)) {
+ tempStream.put(tempFilePath, new FileInputStream(tempFilePath));
+ }
+ return tempStream.get(tempFilePath);
+ }
+
+ /**
+ *
+ * 功能:设置单元格数据,样式 (根据坐标:B3)
+ *
+ *
+ * @param point
+ * @param data
+ * @param sheet
+ * @return
+ * @date 2015年9月27日
+ * @author zq
+ */
+ private TempCell getCell(String point, Object data, Sheet sheet) {
+ TempCell tempCell = new TempCell();
+
+ //得到列 字母
+ String lineStr = "";
+ String reg = "[A-Z]+";
+ Pattern p = Pattern.compile(reg);
+ Matcher m = p.matcher(point);
+ while (m.find()) {
+ lineStr = m.group();
+ }
+ //将列字母转成列号 根据ascii转换
+ char[] ch = lineStr.toCharArray();
+ int column = 0;
+ for (int i = 0; i < ch.length; i++) {
+ char c = ch[i];
+ int post = ch.length - i - 1;
+ int r = (int) Math.pow(10, post);
+ column = column + r * ((int) c - 65);
+ }
+ tempCell.setColumn(column);
+
+ //得到行号
+ reg = "[1-9]+";
+ p = Pattern.compile(reg);
+ m = p.matcher(point);
+ while (m.find()) {
+ tempCell.setRow((Integer.parseInt(m.group()) - 1));
+ }
+
+ //获取模板指定单元格样式,设置到tempCell (写列表数据的时候用)
+ Row rowIn = sheet.getRow(tempCell.getRow());
+ if (rowIn == null) {
+ rowIn = sheet.createRow(tempCell.getRow());
+ }
+ Cell cellIn = rowIn.getCell(tempCell.getColumn());
+ if (cellIn == null) {
+ cellIn = rowIn.createCell(tempCell.getColumn());
+ }
+ tempCell.setCellStyle(cellIn.getCellStyle());
+
+ tempCell.setData(data);
+ return tempCell;
+ }
+
+ /**
+ *
+ * 功能:给指定坐标赋值
+ *
+ *
+ * @param tempCell
+ * @param sheet
+ * @date 2015年9月27日
+ * @author zq
+ */
+ private void setCell(TempCell tempCell, Sheet sheet) {
+ if (tempCell.getColumnSize() > -1) {
+ CellRangeAddress rangeAddress = mergeRegion(sheet, tempCell.getRow(), tempCell.getRow(), tempCell.getColumn(), tempCell.getColumn() + tempCell.getColumnSize());
+ setRegionStyle(tempCell.getCellStyle(), rangeAddress, sheet);
+ }
+
+ Row rowIn = sheet.getRow(tempCell.getRow());
+ if (rowIn == null) {
+ rowIn = sheet.createRow(tempCell.getRow());
+ }
+ Cell cellIn = rowIn.getCell(tempCell.getColumn());
+ if (cellIn == null) {
+ cellIn = rowIn.createCell(tempCell.getColumn());
+ }
+ //根据data类型给cell赋值
+ if (tempCell.getData() instanceof String) {
+ cellIn.setCellValue((String) tempCell.getData());
+ } else if (tempCell.getData() instanceof Integer) {
+ cellIn.setCellValue((int) tempCell.getData());
+ } else if (tempCell.getData() instanceof Double) {
+ cellIn.setCellValue((double) tempCell.getData());
+ } else {
+ cellIn.setCellValue((String) tempCell.getData());
+ }
+ //样式
+ if (tempCell.getCellStyle() != null && tempCell.getColumnSize() == -1) {
+ cellIn.setCellStyle(tempCell.getCellStyle());
+ }
+
+
+ }
+
+ /**
+ *
+ * 功能:写到输出流并移除资源
+ *
+ *
+ * @param tempFilePath
+ * @param os
+ * @throws FileNotFoundException
+ * @throws IOException
+ * @date 2015年9月27日
+ * @author zq
+ */
+ public void writeAndClose(String tempFilePath, OutputStream os) throws FileNotFoundException, IOException {
+ if (getTempWorkbook(tempFilePath) != null) {
+ getTempWorkbook(tempFilePath).write(os);
+ tempWorkbook.remove(tempFilePath);
+ }
+ if (getFileInputStream(tempFilePath) != null) {
+ getFileInputStream(tempFilePath).close();
+ tempStream.remove(tempFilePath);
+ }
+ }
+
+ /**
+ *
+ * 功能:判断指定的单元格是否是合并单元格
+ *
+ *
+ * @param sheet
+ * @param row
+ * @param column
+ * @return 0:不是合并单元格,i:合并单元格的位置
+ * @date 2015年9月27日
+ * @author zq
+ */
+ private Integer isMergedRegion(Sheet sheet, int row, int column) {
+ int sheetMergeCount = sheet.getNumMergedRegions();
+ for (int i = 0; i < sheetMergeCount; i++) {
+ CellRangeAddress range = sheet.getMergedRegion(i);
+ int firstColumn = range.getFirstColumn();
+ int lastColumn = range.getLastColumn();
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ if (row >= firstRow && row <= lastRow) {
+ if (column >= firstColumn && column <= lastColumn) {
+ return i;
+ }
+ }
+ }
+ return -1;
+ }
+
+ /**
+ *
+ * 功能:合并单元格
+ *
+ *
+ * @param sheet
+ * @param firstRow
+ * @param lastRow
+ * @param firstCol
+ * @param lastCol
+ * @date 2015年9月27日
+ * @author zq
+ */
+ private CellRangeAddress mergeRegion(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) {
+ CellRangeAddress rang = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
+ sheet.addMergedRegion(rang);
+ return rang;
+ }
+
+ /**
+ *
+ * 功能:设置合并单元格样式
+ *
+ *
+ * @param cs
+ * @param region
+ * @param sheet
+ * @date 2015年9月27日
+ * @author zq
+ */
+ private static void setRegionStyle(CellStyle cs, CellRangeAddress region, Sheet sheet) {
+ for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
+ Row row = sheet.getRow(i);
+ if (row == null) row = sheet.createRow(i);
+ for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
+ Cell cell = row.getCell(j);
+ if (cell == null) {
+ cell = row.createCell(j);
+ cell.setCellValue("");
+ }
+ cell.setCellStyle(cs);
+ }
+ }
+ }
+
+
+ public static void main(String[] args) throws FileNotFoundException, IOException {
+ String tempFilePath = ExcelUtil.class.getClassLoader().getResource("template1.xlsx").getPath();
+// String tempFilePath = "D:/demo.xlsx";
+ File file = new File("/Users/youyongsheng/Desktop/data.xlsx");
+ OutputStream os = new FileOutputStream(file);
+
+ ExcelUtils excel = new ExcelUtils();
+ Map dataMap = new HashMap();
+ excel.writeData(tempFilePath, dataMap, 0);
+
+ List