1 設計思路
Java 對於Excel 的操作一般借助於POI 類庫,由於有些報表的表頭比較復雜,直接用POI 控制報表的生成比較困難,這時可以先制作Excel 報表模板,而后再通過Java 調用POI 函數將用戶數據寫入到Excel 報表模板,最后導出到新的目標文件即可。
2 設計步驟
2.1 初始步驟
2.1.1創建Excel 報表模板
根據需要設計出Excel 報表,並保存為default.xls。如下圖所示。
2.1.2創建ExcelTemplate類
- /**
- * 該類實現了基於模板的導出
- * 如果要導出序號,需要在excel中定義一個標識為sernums
- * 如果要替換信息,需要傳入一個Map,這個map中存儲着要替換信息的值,在excel中通過#來開頭
- * 要從哪一行那一列開始替換需要定義一個標識為datas
- * 如果要設定相應的樣式,可以在該行使用styles完成設定,此時所有此行都使用該樣式
- * 如果使用defaultStyls作為表示,表示默認樣式,如果沒有defaultStyles使用datas行作為默認樣式
- */
- public class ExcelTemplate {
- private ExcelTemplate() {
- }
- public static ExcelTemplate getInstance() {
- return et;
- }
- }
下面是以后要用到的一些變量和常量
- /**
- * 數據行標識
- */
- public final static String DATA_LINE = "datas";
- /**
- * 默認樣式標識
- */
- public final static String DEFAULT_STYLE = "defaultStyles";
- /**
- * 行樣式標識
- */
- public final static String STYLE = "styles";
- /**
- * 插入序號樣式標識
- */
- public final static String SER_NUM = "sernums";
- private static ExcelTemplate et = new ExcelTemplate();
- private Workbook wb;
- private Sheet sheet;
- /**
- * 數據的初始化列數
- */
- private int initColIndex;
- /**
- * 數據的初始化行數
- */
- private int initRowIndex;
- /**
- * 當前列數
- */
- private int curColIndex;
- /**
- * 當前行數
- */
- private int curRowIndex;
- /**
- * 當前行對象
- */
- private Row curRow;
- /**
- * 最后一行的數據
- */
- private int lastRowIndex;
- /**
- * 默認樣式
- */
- private CellStyle defaultStyle;
- /**
- * 默認行高
- */
- private float rowHeight;
- /**
- * 存儲某一方所對於的樣式
- */
- private Map<Integer,CellStyle> styles;
- /**
- * 序號的列
- */
- private int serColIndex;
2.2 讀取excel報表模板的數據
- /**
- * 1、讀取相應的模板文檔
- */
- public ExcelTemplate readTemplateByClasspath(String path){
- try {
- wb=WorkbookFactory.create(ExcelTemplate.class.getResourceAsStream(path));
- initTemplate();
- } catch (InvalidFormatException e) {
- e.printStackTrace();
- throw new RuntimeException("InvalidFormatException, please check.");
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException("The template is not exist, please check.");
- }
- return this;
- }
- public ExcelTemplate readTemplateByPath(String path){
- try {
- wb=WorkbookFactory.create(new File(path));
- } catch (InvalidFormatException e) {
- e.printStackTrace();
- throw new RuntimeException("InvalidFormatException, please check.");
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException("The template is not exist, please check.");
- }
- return this;
- }
在讀取報表模板的時候會初始化模板,記錄模板的樣式,插入數據的位置
- private void initTemplate(){
- sheet=wb.getSheetAt(0);
- initConfigData();
- lastRowIndex = sheet.getLastRowNum();
- curRow=sheet.getRow(curRowIndex);
- }
- /**
- * 循環遍歷,找到有datas字符的那個單元,記錄initColIndex,initRowIndex,curColIndex,curRowIndex
- * 調用initStyles()方法
- * 在尋找datas字符的時候會順便找一下sernums,如果有則記錄其列號serColIndex;如果沒有則調用initSer()方法,重新循環查找
- */
- private void initConfigData() {
- boolean findData=false;
- boolean findSer = false;
- for(Row row : sheet){
- if(findData) break;
- for(Cell c: row){
- if(c.getCellType()!=Cell.CELL_TYPE_STRING) continue;
- String str=c.getStringCellValue().trim();
- if(str.equals(SER_NUM)){
- serColIndex=c.getColumnIndex();
- findSer=true;
- }
- if(str.equals(DATA_LINE)){
- initColIndex=c.getColumnIndex();
- initRowIndex=row.getRowNum();
- curColIndex=initColIndex;
- curRowIndex=initRowIndex;
- findData=true;
- break;
- }
- }
- }
- if(!findSer){
- initSer();
- }
- initStyles();
- }
- /**
- * 初始化序號位置
- */
- private void initSer() {
- for(Row row:sheet) {
- for(Cell c:row) {
- if(c.getCellType()!=Cell.CELL_TYPE_STRING) continue;
- String str = c.getStringCellValue().trim();
- if(str.equals(SER_NUM)) {
- serColIndex = c.getColumnIndex();
- }
- }
- }
- }
- /**
- * 初始化樣式信息
- */
- private void initStyles(){
- styles = new HashMap<Integer, CellStyle>();
- for(Row row:sheet) {
- for(Cell c:row) {
- if(c.getCellType()!=Cell.CELL_TYPE_STRING) continue;
- String str = c.getStringCellValue().trim();
- if(str.equals(DEFAULT_STYLE)) {
- defaultStyle = c.getCellStyle();
- rowHeight=row.getHeightInPoints();
- }
- if(str.equals(STYLE)||str.equals(SER_NUM)) {
- styles.put(c.getColumnIndex(), c.getCellStyle());
- }
- }
- }
- }
2.3 新建excel並向其寫數據
- public void writeToFile(String filepath){
- FileOutputStream fos=null;
- try {
- fos=new FileOutputStream(filepath);
- wb.write(fos);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- throw new RuntimeException("寫入的文件不存在"+e.getMessage());
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException("寫入數據失敗"+e.getMessage());
- } finally{
- if(fos!=null)
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
2.4 實現Excel公共模板的第一步(實現了數據插入)
下面是創建單元格的方法,以及重載方法
- public void createCell(String value){
- Cell c =curRow.createCell(curColIndex);
- setCellStyle(c);
- c.setCellValue(value);
- curColIndex++;
- }
- public void createCell(int value) {
- Cell c = curRow.createCell(curColIndex);
- setCellStyle(c);
- c.setCellValue((int)value);
- curColIndex++;
- }
- public void createCell(Date value) {
- Cell c = curRow.createCell(curColIndex);
- setCellStyle(c);
- c.setCellValue(value);
- curColIndex++;
- }
- public void createCell(double value) {
- Cell c = curRow.createCell(curColIndex);
- setCellStyle(c);
- c.setCellValue(value);
- curColIndex++;
- }
- public void createCell(boolean value) {
- Cell c = curRow.createCell(curColIndex);
- setCellStyle(c);
- c.setCellValue(value);
- curColIndex++;
- }
- public void createCell(Calendar value) {
- Cell c = curRow.createCell(curColIndex);
- setCellStyle(c);
- c.setCellValue(value);
- curColIndex++;
- }
上面的方法會調用setCellStyle方法來設置單元格樣式
- /**
- * 設置某個單元格的樣式
- * @param c
- */
- private void setCellStyle(Cell c) {
- if(styles.containsKey(c.getColumnIndex())) {
- c.setCellStyle(styles.get(c.getColumnIndex()));
- } else {
- c.setCellStyle(defaultStyle);
- }
- }
createNewRow方法用於創建新的行,新的行的位置位於curRowIndex,從curRowIndex到lastRowIndex的所有行會自動向下移動一行。
- public void createNewRow(){
- if(lastRowIndex>curRowIndex&&curRowIndex!=initRowIndex) {
- sheet.shiftRows(curRowIndex, lastRowIndex, 1,true,true);
- lastRowIndex++;
- }
- curRow = sheet.createRow(curRowIndex);
- curRow.setHeightInPoints(rowHeight);
- curRowIndex++;
- curColIndex=initColIndex;
- }
2.5 實現增加序號
- /**
- * 插入序號,會自動找相應的序號標示的位置完成插入
- */
- public void insertSer() {
- int index = 1;
- Row row = null;
- Cell c = null;
- for(int i=initRowIndex;i<curRowIndex;i++) {
- row = sheet.getRow(i);
- c = row.createCell(serColIndex);
- setCellStyle(c);
- c.setCellValue(index++);
- }
- }
2.6 替換模板中#開頭的值
- /**
- * 根據map替換相應的常量,通過Map中的值來替換#開頭的值
- * @param datas
- */
- public void replaceFinalData(Map<String,String> datas) {
- if(datas==null) return;
- for(Row row:sheet) {
- for(Cell c:row) {
- if(c.getCellType()!=Cell.CELL_TYPE_STRING) continue;
- String str = c.getStringCellValue().trim();
- if(str.startsWith("#")) {
- if(datas.containsKey(str.substring(1))) {
- c.setCellValue(datas.get(str.substring(1)));
- }
- }
- }
- }
- }
3實現步驟
- @Test
- public void test01() {
- ExcelTemplate et = ExcelTemplate.getInstance()
- .readTemplateByClasspath("/excel/default.xls");
- et.createNewRow();
- et.createCell("1111111");
- et.createCell("aaaaaaaaaaaa");
- et.createCell("a1");
- et.createCell("a2a2");
- et.createNewRow();
- et.createCell("222222");
- et.createCell("bbbbb");
- et.createCell("b");
- et.createCell("dbbb");
- et.createNewRow();
- et.createCell("3333333");
- et.createCell("cccccc");
- et.createCell("a1");
- et.createCell(12333);
- et.createNewRow();
- et.createCell("4444444");
- et.createCell("ddddd");
- et.createCell("a1");
- et.createCell("a2a2");
- et.createNewRow();
- et.createCell("555555");
- et.createCell("eeeeee");
- et.createCell("a1");
- et.createCell(112);
- et.createNewRow();
- et.createCell("555555");
- et.createCell("eeeeee");
- et.createCell("a1");
- et.createCell("a2a2");
- et.createNewRow();
- et.createCell("555555");
- et.createCell("eeeeee");
- et.createCell("a1");
- et.createCell("a2a2");
- et.createNewRow();
- et.createCell("555555");
- et.createCell("eeeeee");
- et.createCell("a1");
- et.createCell("a2a2");
- et.createNewRow();
- et.createCell("555555");
- et.createCell("eeeeee");
- et.createCell("a1");
- et.createCell("a2a2");
- et.createNewRow();
- et.createCell("555555");
- et.createCell("eeeeee");
- et.createCell("a1");
- et.createCell("a2a2");
- et.createNewRow();
- et.createCell("555555");
- et.createCell("eeeeee");
- et.createCell("a1");
- et.createCell("a2a2");
- Map<String,String> datas = new HashMap<String,String>();
- datas.put("title","測試用戶信息");
- datas.put("date","2012-06-02 12:33");
- datas.put("dep","昭通師專財務處");
- et.replaceFinalData(datas);
- et.insertSer();
- et.writeToFile("d:/test/poi/test01.xls");
- }
4 最后結果