POI生成EXCEL文件(字体样式单元格合并计算公式)

Posted newwind

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POI生成EXCEL文件(字体样式单元格合并计算公式)相关的知识,希望对你有一定的参考价值。

  创建一个封装类:

  1 package com.jason.excel;
  2 
  3 import java.io.FileNotFoundException;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 
  7 import org.apache.poi.hssf.usermodel.HSSFCell;
  8 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  9 import org.apache.poi.hssf.usermodel.HSSFFont;
 10 import org.apache.poi.hssf.usermodel.HSSFRow;
 11 import org.apache.poi.hssf.usermodel.HSSFSheet;
 12 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 13 import org.apache.poi.ss.usermodel.Cell;
 14 import org.apache.poi.ss.usermodel.CellValue;
 15 import org.apache.poi.ss.usermodel.FormulaEvaluator;
 16 import org.apache.poi.ss.util.CellRangeAddress;
 17 
 18 
 19 /**
 20  * 生成可以合并的单元格的Excel
 21  * @function  
 22  * @author 小风微凉
 23  * @time  2018-8-9 下午8:02:30
 24  */
 25 public class CreateExcelUtil {
 26     
 27     /**
 28      * 生成字体(HSSFFont)
 29      * @param workBook  excel对象(必输项)
 30      * @param fontName  字体名称(选填项,可直接null默认处理)
 31      * @param fontSize  字体大小(选填项,可直接-1,默认处理)
 32      * @param fontColor 字体颜色(选填项,可直接-1,默认处理)
 33      * @return
 34      */
 35     public static HSSFFont getFont(HSSFWorkbook workBook,String fontName,int fontSize,int fontColor){
 36         HSSFFont font=workBook.createFont();
 37         if(fontName==null){
 38             font.setFontName("宋体");                
 39         }else{
 40             font.setFontName(fontName);    
 41         }
 42         if(fontSize<=0){
 43             font.setFontHeightInPoints((short) 14);// 设置字体大小            
 44         }else{
 45             font.setFontHeightInPoints((short) fontSize);// 设置字体大小            
 46         }
 47         if(fontColor>0){
 48             font.setColor((short) fontColor);
 49         }        
 50         return font;
 51     }
 52     /**
 53      * 获取样式(HSSFCellStyle)
 54      * @param workBook excel对象(必输项)
 55      * @param lfc       样式对齐类型,默认null居中,lfc="left"   lfc="right"  lfc="center"
 56      * @return
 57      */
 58     public static HSSFCellStyle getStyle(HSSFWorkbook workBook,HSSFFont font,String lfc){
 59         HSSFCellStyle style=workBook.createCellStyle();
 60         //默认上下居中
 61         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中   
 62         if(lfc==null || "center".equals(lfc)){
 63             style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//字体居中
 64         }else if("left".equals(lfc)){
 65             style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//字体居左 
 66         }else if("right".equals(lfc)){
 67             style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//字体居右   
 68         }else{
 69             style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//字体居中
 70         }
 71         if(font!=null){
 72             style.setFont(font);
 73         }
 74         return style;        
 75     }
 76     /**
 77      * 合并行,合并列处理
 78      * @param sheet 当前sheet对象
 79      * @param fromRowIndex  从第*行开始(起始索引值为0)
 80      * @param toRowIndex    到第*行结束
 81      * @param fromColIndex  从第*列开始(起始索引值为0)
 82      * @param toColIndex    到第*列结束
 83      * @see 举例:setMergeArea(sheet,0,0,0,1):合并sheet下的第一行,前2列
 84      */
 85     public static void setMergeArea(HSSFSheet sheet,int fromRowIndex,int toRowIndex,int fromColIndex,int toColIndex){
 86         sheet.addMergedRegion(new CellRangeAddress(fromRowIndex,toRowIndex,fromColIndex,toColIndex));
 87     }
 88     
 89     /**
 90      * 生成指定行的单元格
 91      * @param row    指定行对象
 92      * @param cellIndex   单元格在所在行的列索引值
 93      * @param style  单元格样式对象
 94      * @param info   单元格内的信息
 95      * @return
 96      */
 97     public static HSSFCell createCell(HSSFRow row,int cellIndex,HSSFCellStyle style,String info){
 98         HSSFCell cell=row.createCell(cellIndex);
 99         if(style!=null){
100             cell.setCellStyle(style);
101         }
102         if(!"".equals(info) && info!=null){
103             cell.setCellValue(info);
104         }
105         return cell;
106     }
107     /**
108      * 获取默认sheet对象
109      * @param workBook  excel对象 
110      * @param defaultColWidth  列宽(字符个数宽度)
111      * @return
112      */
113     public static HSSFSheet getDefaultSheet(HSSFWorkbook workBook,int defaultColWidth){
114         HSSFSheet sheet = workBook.createSheet();        
115         //设置默认列宽
116         if(defaultColWidth<=0){
117             sheet.setDefaultColumnWidth(13);  
118         }else{
119             sheet.setDefaultColumnWidth(defaultColWidth);  
120         }
121         return sheet;
122     }
123     /**
124      * 生成一个单元行对象
125      * @param sheet sheet对象
126      * @param rowIndex  生成的行索引值
127      * @param defaultHeight 设置默认行的高度
128      * @return
129      */
130     public static HSSFRow creatRow(HSSFSheet sheet,int rowIndex,int defaultHeight){
131         HSSFRow row=sheet.createRow(rowIndex);
132         if(defaultHeight>0){
133             row.setHeight((short)defaultHeight);
134         }
135         return row;
136     }
137     /**
138      * 获取自带统计功能的单元格对象
139      * @param workBook excel对象
140      * @param row    指定行对象
141      * @param cellIndex   单元格在所在行的列索引值
142      * @param style  单元格样式对象
143      * @param info   单元格内的信息
144      * @param formula 计算公式
145      * @return
146      */
147     public static  HSSFCell createCellFormula(HSSFWorkbook workBook,HSSFRow row,int cellIndex,HSSFCellStyle style,String info,String formula){
148         HSSFCell cell=row.createCell(cellIndex);
149         if(style!=null){
150             cell.setCellStyle(style);
151         }
152         if(!"".equals(info) && info!=null){
153             cell.setCellValue(info);
154         }
155         cell.setCellType(Cell.CELL_TYPE_FORMULA);
156         cell.setCellFormula(formula);//计算公式
157         FormulaEvaluator evaluator = workBook.getCreationHelper().createFormulaEvaluator();
158         evaluator.evaluateFormulaCell(cell);
159         CellValue cellValue = evaluator.evaluate(cell);
160         return cell;
161     }
162     /**
163      * 统计计算公式(+)求和
164      * @param tagert  列标记
165      * @param begIndex   起始索引
166      * @param endIndex   终止索引
167      * @return    返回计算公式
168      */
169     public static String getSumStr(String tagert,int begIndex,int endIndex){
170         String retStr=tagert+begIndex;
171         for(int i=begIndex+1;i<=endIndex;i++ ){
172             retStr+=("+"+tagert+i);
173         }
174         return "SUM("+retStr+")";
175     }
176     /**
177      * 生产excel文件到指定的路径下
178      * @param workBook excel对象
179      * @param sheetName sheet名称
180      * @param filePathName 保存路径
181      */
182     public static void writeToExcelFile(HSSFWorkbook workBook,String sheetName,String filePathName){
183         workBook.setSheetName(0,sheetName);
184         FileOutputStream fileOut = null;
185         try {
186             fileOut = new FileOutputStream(filePathName);
187         } catch (FileNotFoundException e1) {
188             e1.printStackTrace();//异常信息需要日志记录
189         }
190         try {
191             workBook.write( fileOut );
192         } catch (IOException e) {
193             e.printStackTrace();
194         }
195     }
196 }

测试类:

  1 package com.jason.test;
  2 
  3 import java.io.FileNotFoundException;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 
  7 import org.apache.poi.hssf.usermodel.HSSFCell;
  8 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  9 import org.apache.poi.hssf.usermodel.HSSFFont;
 10 import org.apache.poi.hssf.usermodel.HSSFRow;
 11 import org.apache.poi.hssf.usermodel.HSSFSheet;
 12 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 13 import org.apache.poi.ss.usermodel.Cell;
 14 import org.apache.poi.ss.usermodel.CellValue;
 15 import org.apache.poi.ss.usermodel.FormulaEvaluator;
 16 import org.apache.poi.ss.util.CellRangeAddress;
 17 
 18 import com.jason.excel.CreateExcelUtil;
 19 
 20 public class Test2 {
 21     /**
 22      * 每个报表都不相同
 23      */
 24     private static String[] arr={"B","C","D","E","F","G","H","I","J","K","L"};
 25     public static void main(String[] args)throws FileNotFoundException, IOException {
 26         // 创建HSSFWorkbook对象
 27         HSSFWorkbook workBook = new HSSFWorkbook();
 28         //创建sheet对象
 29         HSSFSheet sheet = CreateExcelUtil.getDefaultSheet(workBook,13);        
 30         //创建字体和样式
 31         HSSFFont font=CreateExcelUtil.getFont(workBook, null, -1, -1);
 32         HSSFCellStyle style=CreateExcelUtil.getStyle(workBook, font,"center");//居中样式
 33         HSSFCellStyle style_left=CreateExcelUtil.getStyle(workBook,font, "left");//居左样式
 34         
 35         //创建第1行
 36         CreateExcelUtil.setMergeArea(sheet, 0,0,0,11);
 37         HSSFRow row_1=CreateExcelUtil.creatRow(sheet, 0, 500);
 38         HSSFCell cell_1_1=CreateExcelUtil.createCell(row_1, 0, style, "企业网银关键业务统计报表");
 39         //创建第2行
 40         CreateExcelUtil.setMergeArea(sheet, 1,1,1,3);
 41         CreateExcelUtil.setMergeArea(sheet, 1,1,5,11);
 42         HSSFRow row_2=CreateExcelUtil.creatRow(sheet, 1, 400);
 43         //报表类型
 44         HSSFCell cell_2_1=CreateExcelUtil.createCell(row_2, 0, style, "报表类型");
 45         HSSFCell cell_2_2=CreateExcelUtil.createCell(row_2, 1, style_left,"日报表" );
 46         //统计时间
 47         HSSFCell cell_2_3=CreateExcelUtil.createCell(row_2, 4, style, "统计时间");
 48         HSSFCell cell_2_4=CreateExcelUtil.createCell(row_2, 5, style_left, "2018/08/08-2018/08/09");
 49         //创建第3行
 50         CreateExcelUtil.setMergeArea(sheet, 2,2,1,3);
 51         CreateExcelUtil.setMergeArea(sheet, 2,2,5,11);
 52         HSSFRow row_3=CreateExcelUtil.creatRow(sheet, 2, -1);
 53         //机构类型
 54         HSSFCell cell_3_1=CreateExcelUtil.createCell(row_3, 0, style, "机构类型");
 55         HSSFCell cell_3_2=CreateExcelUtil.createCell(row_3, 1, style_left,"分行" );
 56         //机构名称
 57         HSSFCell cell_3_3=CreateExcelUtil.createCell(row_3, 4, style, "机构名称");
 58         HSSFCell cell_3_4=CreateExcelUtil.createCell(row_3, 5, style_left,"云南红塔银行总行营业部汇总" );
 59         //创建第4行
 60         CreateExcelUtil.setMergeArea(sheet,3,3,1,11);
 61         HSSFRow row_4=CreateExcelUtil.creatRow(sheet, 3, -1);
 62         //统计渠道
 63         HSSFCell cell_4_1=CreateExcelUtil.createCell(row_4, 0, style, "统计渠道");
 64         HSSFCell cell_4_2=CreateExcelUtil.createCell(row_4, 1, style_left,"全部" );
 65         //创建第5行
 66         CreateExcelUtil.setMergeArea(sheet, 4,4,0,11);
 67         HSSFRow row_5=CreateExcelUtil.creatRow(sheet, 4, -1);
 68         //创建第6行和第7行        
 69         CreateExcelUtil.setMergeArea(sheet, 5,6,0,1);//一级业务
 70         CreateExcelUtil.setMergeArea(sheet, 5,6,2,3);//二级业务 
 71         CreateExcelUtil.setMergeArea(sheet, 5,5,4,5);//成功数  
 72         CreateExcelUtil.setMergeArea(sheet, 5,5,6,7);//失败数   
 73         CreateExcelUtil.setMergeArea(sheet, 5,5,8,9);//成功金额  
 74         CreateExcelUtil.setMergeArea(sheet, 5,5,10,11);//失败金额   
 75         HSSFRow row_6=CreateExcelUtil.creatRow(sheet, 5, -1);
 76         HSSFRow row_7=CreateExcelUtil.creatRow(sheet, 6, -1);
 77         //一级业务
 78         HSSFCell cell_6_1=CreateExcelUtil.createCell(row_6, 0, style, "一级业务");
 79         //二级业务
 80         HSSFCell cell_6_2=CreateExcelUtil.createCell(row_6, 2, style, "二级业务");
 81         //成功数
 82         HSSFCell cell_6_3=CreateExcelUtil.createCell(row_6, 4, style, "成功数");
 83         //单位个人
 84         HSSFCell cell_7_1=CreateExcelUtil.createCell(row_7, 4, style, "单位");
 85         HSSFCell cell_7_2=CreateExcelUtil.createCell(row_7, 5, style, "个人");
 86         //失败数
 87         HSSFCell cell_6_4=CreateExcelUtil.createCell(row_6, 6, style, "失败数");
 88         //单位个人
 89         HSSFCell cell_7_3=CreateExcelUtil.createCell(row_7, 6, style, "单位");
 90         HSSFCell cell_7_4=CreateExcelUtil.createCell(row_7, 7, style, "个人");
 91         //成功金额
 92         HSSFCell cell_6_5=CreateExcelUtil.createCell(row_6, 8, style, "成功金额");
 93         //单位个人
 94         HSSFCell cell_7_5=CreateExcelUtil.createCell(row_7, 8, style, "单位");
 95         HSSFCell cell_7_6=CreateExcelUtil.createCell(row_7, 9, style, "个人");
 96         //失败金额
 97         HSSFCell cell_6_6=CreateExcelUtil.createCell(row_6, 10, style, "失败金额");
 98         //单位个人
 99         HSSFCell cell_7_7=CreateExcelUtil.createCell(row_7, 10, style, "单位");
100         HSSFCell cell_7_8=CreateExcelUtil.createCell(row_7, 11, style, "个人");
101         
102         
103         //循环处理数据
104         int rowIndex=7;//从第7行开始计算
105         int dataCount=7;
106         for(int i=0;i<10;i++,rowIndex++){//循环产生行数据
107             dataCount++;
108             //创建新的行
109             HSSFRow row_index=CreateExcelUtil.creatRow(sheet, rowIndex, -1);
110             HSSFCell cell_1=CreateExcelUtil.createCell(row_index, 0, style, "世博支行");
111             for(int j=1;j<12;j++){//循环产生列数据
112                 HSSFCell cell=CreateExcelUtil.createCell(row_index, j, style, "100");
113             }
114         }
115         
116         //合计统计
117         HSSFRow row_total=CreateExcelUtil.creatRow(sheet, rowIndex, -1);
118         HSSFCell cell_total=CreateExcelUtil.createCell(row_total, 0, style, "合计");
119         for(int i=0;i<arr.length;i++){            
120             String formula=CreateExcelUtil.getSumStr(arr[i],8,dataCount);
121             HSSFCell cell=CreateExcelUtil.createCellFormula(workBook,row_total, i+1, style, null,formula);
122         }
123         //开始写入文件: 定义你需要的输出流
124         CreateExcelUtil.writeToExcelFile(workBook, "企业网银关键业务统计报表", "./test.xls");
125     }    
126 }

 生成结果图:

技术分享图片

未封装的代码:

技术分享图片
  1 package com.jason.test;
  2 
  3 import java.io.FileNotFoundException;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 
  7 import org.apache.poi.hssf.usermodel.HSSFCell;
  8 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  9 import org.apache.poi.hssf.usermodel.HSSFFont;
 10 import org.apache.poi.hssf.usermodel.HSSFRow;
 11 import org.apache.poi.hssf.usermodel.HSSFSheet;
 12 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 13 import org.apache.poi.hssf.util.HSSFColor;
 14 import org.apache.poi.ss.usermodel.Cell;
 15 import org.apache.poi.ss.usermodel.CellStyle;
 16 import org.apache.poi.ss.usermodel.CellValue;
 17 import org.apache.poi.ss.usermodel.FormulaEvaluator;
 18 import org.apache.poi.ss.util.CellRangeAddress;
 19 
 20 public class Test {
 21     /**
 22      * 每个报表都不相同
 23      */
 24     private static String[] arr={"B","C","D","E","F","G","H","I","J","K","L"};
 25     public static void main(String[] args)throws FileNotFoundException, IOException {
 26         // 创建HSSFWorkbook对象
 27         HSSFWorkbook workBook = new HSSFWorkbook();
 28         //创建sheet对象
 29         HSSFSheet sheet = workBook.createSheet();        
 30         //设置默认列宽
 31         sheet.setDefaultColumnWidth(13);   
 32         //创建字体和样式
 33         HSSFFont font=workBook.createFont();
 34         HSSFCellStyle style=workBook.createCellStyle();//居中样式
 35         HSSFCellStyle style_left=workBook.createCellStyle();
 36         
 37        /**
 38         * 设置边框样式
 39         *  style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
 40         *  style.setBottomBorderColor(HSSFColor.BLACK.index);
 41         *  style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
 42         *  style.setLeftBorderColor(HSSFColor.BLACK.index);
 43         *  style.setBorderRight(HSSFCellStyle.BORDER_THIN);
 44         *  style.setRightBorderColor(HSSFColor.BLACK.index);
 45         *  style.setBorderTop(HSSFCellStyle.BORDER_THIN);
 46         *  style.setTopBorderColor(HSSFColor.BLACK.index);
 47         */
 48         
 49         
 50         /**
 51          * 设置背景色样式
 52          * style.setFillForegroundColor(HSSFColor.LEMON_CHIFFON.index);
 53          * style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//单元格模式[SOLID_FOREGROUND]
 54          */
 55         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置字体居中(OK)
 56         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中   
 57         style_left.setVerticalAlignment(HSSFCellStyle.ALIGN_LEFT);//上下居左  
 58         
 59         font.setFontName("宋体");        
 60         font.setFontHeightInPoints((short) 14);// 设置字体大小
 61         style.setFont(font);//设置字体样式
 62         style_left.setFont(font);//设置字体样式
 63         //创建第1行
 64         sheet.addMergedRegion(new CellRangeAddress(0,0,0,11));
 65         HSSFRow row_1=sheet.createRow(0);
 66         //设置行高
 67         row_1.setHeight((short)500);
 68         HSSFCell cell_1_1=row_1.createCell(0);
 69         cell_1_1.setCellStyle(style);
 70         cell_1_1.setCellValue("企业网银关键业务统计报表");        
 71         //创建第2行
 72         sheet.addMergedRegion(new CellRangeAddress(1,1,1,3));
 73         sheet.addMergedRegion(new CellRangeAddress(1,1,5,11));
 74         HSSFRow row_2=sheet.createRow(1);
 75         row_2.setHeight((short)400);
 76         //报表类型
 77         HSSFCell cell_2_1=row_2.createCell(0);
 78         cell_2_1.setCellStyle(style);
 79         cell_2_1.setCellValue("报表类型");
 80         HSSFCell cell_2_2=row_2.createCell(1);
 81         cell_2_2.setCellStyle(style_left);
 82         cell_2_2.setCellValue("日报表");
 83         //统计时间
 84         HSSFCell cell_2_3=row_2.createCell(4);
 85         cell_2_3.setCellStyle(style);
 86         cell_2_3.setCellValue("统计时间");
 87         HSSFCell cell_2_4=row_2.createCell(5);
 88         cell_2_4.setCellStyle(style_left);
 89         cell_2_4.setCellValue("2018/08/08-2018/08/09");
 90         //创建第3行
 91         sheet.addMergedRegion(new CellRangeAddress(2,2,1,3));
 92         sheet.addMergedRegion(new CellRangeAddress(2,2,5,11));
 93         HSSFRow row_3=sheet.createRow(2);
 94         //机构类型
 95         HSSFCell cell_3_1=row_3.createCell(0);
 96         cell_3_1.setCellStyle(style);
 97         cell_3_1.setCellValue("机构类型");
 98         HSSFCell cell_3_2=row_3.createCell(1);
 99         cell_3_2.setCellStyle(style_left);
100         cell_3_2.setCellValue("分行");
101         //机构名称
102         HSSFCell cell_3_3=row_3.createCell(4);
103         cell_3_3.setCellStyle(style);
104         cell_3_3.setCellValue("机构名称");
105         HSSFCell cell_3_4=row_3.createCell(5);
106         cell_3_4.setCellStyle(style_left);
107         cell_3_4.setCellValue("云南红塔银行总行营业部汇总");
108         //创建第4行
109         sheet.addMergedRegion(new CellRangeAddress(3,3,1,11));
110         HSSFRow row_4=sheet.createRow(3);
111         //统计渠道
112         HSSFCell cell_4_1=row_4.createCell(0);
113         cell_4_1.setCellStyle(style);
114         cell_4_1.setCellValue("统计渠道");
115         HSSFCell cell_4_2=row_4.createCell(1);
116         cell_4_2.setCellStyle(style_left);
117         cell_4_2.setCellValue("全部");
118         //创建第5行
119         sheet.addMergedRegion(new CellRangeAddress(4,4,0,11));
120         HSSFRow row_5=sheet.createRow(4);
121         //创建第6行和第7行
122         sheet.addMergedRegion(new CellRangeAddress(5,6,0,1));//一级业务
123         sheet.addMergedRegion(new CellRangeAddress(5,6,2,3));//二级业务
124         sheet.addMergedRegion(new CellRangeAddress(5,5,4,5));//成功数
125         sheet.addMergedRegion(new CellRangeAddress(5,5,6,7));//失败数
126         sheet.addMergedRegion(new CellRangeAddress(5,5,8,9));//成功金额
127         sheet.addMergedRegion(new CellRangeAddress(5,5,10,11));//失败金额
128         HSSFRow row_6=sheet.createRow(5);
129         HSSFRow row_7=sheet.createRow(6);
130         //一级业务
131         HSSFCell cell_6_1=row_6.createCell(0);
132         cell_6_1.setCellStyle(style);
133         cell_6_1.setCellValue("一级业务");
134         //二级业务
135         HSSFCell cell_6_2=row_6.createCell(2);
136         cell_6_2.setCellStyle(style);
137         cell_6_2.setCellValue("二级业务");
138         //成功数
139         HSSFCell cell_6_3=row_6.createCell(4);
140         cell_6_3.setCellStyle(style);
141         cell_6_3.setCellValue("成功数");
142         //单位个人
143         HSSFCell cell_7_1=row_7.createCell(4);
144         cell_7_1.setCellStyle(style);
145         cell_7_1.setCellValue("单位");
146         HSSFCell cell_7_2=row_7.createCell(5);
147         cell_7_2.setCellStyle(style);
148         cell_7_2.setCellValue("个人");
149         //失败数
150         HSSFCell cell_6_4=row_6.createCell(6);
151         cell_6_4.setCellStyle(style);
152         cell_6_4.setCellValue("失败数");
153         //单位个人
154         HSSFCell cell_7_3=row_7.createCell(6);
155         cell_7_3.setCellStyle(style);
156         cell_7_3.setCellValue("单位");
157         HSSFCell cell_7_4=row_7.createCell(7);
158         cell_7_4.setCellStyle(style);
159         cell_7_4.setCellValue("个人");
160         //成功金额
161         HSSFCell cell_6_5=row_6.createCell(8);
162         cell_6_5.setCellStyle(style);
163         cell_6_5.setCellValue("成功金额");
164         //单位个人
165         HSSFCell cell_7_5=row_7.createCell(8);
166         cell_7_5.setCellStyle(style);
167         cell_7_5.setCellValue("单位");
168         HSSFCell cell_7_6=row_7.createCell(9);
169         cell_7_6.setCellStyle(style);
170         cell_7_6.setCellValue("个人");
171         //失败金额
172         HSSFCell cell_6_6=row_6.createCell(10);
173         cell_6_6.setCellStyle(style);
174         cell_6_6.setCellValue("失败金额");
175         //单位个人
176         HSSFCell cell_7_7=row_7.createCell(10);
177         cell_7_7.setCellStyle(style);
178         cell_7_7.setCellValue("单位");
179         HSSFCell cell_7_8=row_7.createCell(11);
180         cell_7_8.setCellStyle(style);
181         cell_7_8.setCellValue("个人");
182         
183         
184         //循环处理数据
185         int rowIndex=7;
186         int dataCount=7;
187         for(int i=0;i<10;i++,rowIndex++){
188             dataCount++;
189             //创建新的行
190             HSSFRow row_index=sheet.createRow(rowIndex);
191             HSSFCell cell_1=row_index.createCell(0);
192             cell_1.setCellStyle(style);
193             cell_1.setCellValue("世博支行");            
194             for(int j=1;j<12;j++){
195                 HSSFCell cell=row_index.createCell(j);
196                 cell.setCellStyle(style);
197                 cell.setCellValue("100");        
198             }
199         }
200         
201         //合计统计
202         HSSFRow row_total=sheet.createRow(rowIndex);
203         HSSFCell cell_total=row_total.createCell(0);
204         cell_total.setCellStyle(style);
205         cell_total.setCellValue("合计");    
206         for(int i=0;i<arr.length;i++){            
207             HSSFCell cell=row_total.createCell(i+1);
208             cell.setCellStyle(style);
209             //开始统计
210             cell.setCellType(Cell.CELL_TYPE_FORMULA);
211             cell.setCellFormula(getSumStr(arr[i],8,dataCount));//计算公式
212             System.out.println(getSumStr(arr[i],8,dataCount));
213             //创建poi计算器
214             FormulaEvaluator evaluator = workBook.getCreationHelper().createFormulaEvaluator();
215             evaluator.evaluateFormulaCell(cell);
216             CellValue cellValue = evaluator.evaluate(cell);
217         }
218         //开始写入文件: 定义你需要的输出流
219         workBook.setSheetName(0,"企业网银关键业务统计报表");
220         FileOutputStream fileOut =new FileOutputStream("./test.xls");
221         workBook.write( fileOut );
222     }
223     /**
224      * 统计计算公式
225      * @param tagert  列标记
226      * @param begIndex   起始索引
227      * @param endIndex   终止索引
228      * @return    返回计算公式
229      */
230     public static String getSumStr(String tagert,int begIndex,int endIndex){
231         String retStr=tagert+begIndex;
232         for(int i=begIndex+1;i<=endIndex;i++ ){
233             retStr+=("+"+tagert+i);
234         }
235         return "SUM("+retStr+")";
236     }
237 }
View Code

 

以上是关于POI生成EXCEL文件(字体样式单元格合并计算公式)的主要内容,如果未能解决你的问题,请参考以下文章

POI 导出 Excel:字体颜色行列自适应锁住合并单元格一文搞定……

java POI excel 导出复合样式(一个单元格两个字体)

POI 导出 Excel:字体颜色行列自适应锁住合并单元格……

POI导出excel:设置字体颜色行高自适应列宽自适应锁住单元格合并单元格...

poi怎么将单元格字体设置为斜体

Apache-poi:自动设置合并单元格的大小并使字体变为粗体