POI生成EXCEL文件(字体、样式、单元格合并、计算公式)
创建一个封装类:
package com.jason.excel;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.util.CellRangeAddress;
/**
* 生成可以合并的单元格的Excel
* @function
* @author 小风微凉
* @time 2018-8-9 下午8:02:30
*/
public class CreateExcelUtil {
/**
* 生成字体(HSSFFont)
* @param workBook excel对象(必输项)
* @param fontName 字体名称(选填项,可直接null默认处理)
* @param fontSize 字体大小(选填项,可直接-1,默认处理)
* @param fontColor 字体颜色(选填项,可直接-1,默认处理)
* @return
*/
public static HSSFFont getFont(HSSFWorkbook workBook,String fontName,int fontSize,int fontColor){
HSSFFont font=workBook.createFont();
if(fontName==null){
font.setFontName("宋体");
}else{
font.setFontName(fontName);
}
if(fontSize<=0){
font.setFontHeightInPoints((short) 14);// 设置字体大小
}else{
font.setFontHeightInPoints((short) fontSize);// 设置字体大小
}
if(fontColor>0){
font.setColor((short) fontColor);
}
return font;
}
/**
* 获取样式(HSSFCellStyle)
* @param workBook excel对象(必输项)
* @param lfc 样式对齐类型,默认null居中,lfc="left" lfc="right" lfc="center"
* @return
*/
public static HSSFCellStyle getStyle(HSSFWorkbook workBook,HSSFFont font,String lfc){
HSSFCellStyle style=workBook.createCellStyle();
//默认上下居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
if(lfc==null || "center".equals(lfc)){
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//字体居中
}else if("left".equals(lfc)){
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//字体居左
}else if("right".equals(lfc)){
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//字体居右
}else{
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//字体居中
}
if(font!=null){
style.setFont(font);
}
return style;
}
/**
* 合并行,合并列处理
* @param sheet 当前sheet对象
* @param fromRowIndex 从第*行开始(起始索引值为0)
* @param toRowIndex 到第*行结束
* @param fromColIndex 从第*列开始(起始索引值为0)
* @param toColIndex 到第*列结束
* @see 举例:setMergeArea(sheet,0,0,0,1):合并sheet下的第一行,前2列
*/
public static void setMergeArea(HSSFSheet sheet,int fromRowIndex,int toRowIndex,int fromColIndex,int toColIndex){
sheet.addMergedRegion(new CellRangeAddress(fromRowIndex,toRowIndex,fromColIndex,toColIndex));
}
/**
* 生成指定行的单元格
* @param row 指定行对象
* @param cellIndex 单元格在所在行的列索引值
* @param style 单元格样式对象
* @param info 单元格内的信息
* @return
*/
public static HSSFCell createCell(HSSFRow row,int cellIndex,HSSFCellStyle style,String info){
HSSFCell cell=row.createCell(cellIndex);
if(style!=null){
cell.setCellStyle(style);
}
if(!"".equals(info) && info!=null){
cell.setCellValue(info);
}
return cell;
}
/**
* 获取默认sheet对象
* @param workBook excel对象
* @param defaultColWidth 列宽(字符个数宽度)
* @return
*/
public static HSSFSheet getDefaultSheet(HSSFWorkbook workBook,int defaultColWidth){
HSSFSheet sheet = workBook.createSheet();
//设置默认列宽
if(defaultColWidth<=0){
sheet.setDefaultColumnWidth(13);
}else{
sheet.setDefaultColumnWidth(defaultColWidth);
}
return sheet;
}
/**
* 生成一个单元行对象
* @param sheet sheet对象
* @param rowIndex 生成的行索引值
* @param defaultHeight 设置默认行的高度
* @return
*/
public static HSSFRow creatRow(HSSFSheet sheet,int rowIndex,int defaultHeight){
HSSFRow row=sheet.createRow(rowIndex);
if(defaultHeight>0){
row.setHeight((short)defaultHeight);
}
return row;
}
/**
* 获取自带统计功能的单元格对象
* @param workBook excel对象
* @param row 指定行对象
* @param cellIndex 单元格在所在行的列索引值
* @param style 单元格样式对象
* @param info 单元格内的信息
* @param formula 计算公式
* @return
*/
public static HSSFCell createCellFormula(HSSFWorkbook workBook,HSSFRow row,int cellIndex,HSSFCellStyle style,String info,String formula){
HSSFCell cell=row.createCell(cellIndex);
if(style!=null){
cell.setCellStyle(style);
}
if(!"".equals(info) && info!=null){
cell.setCellValue(info);
}
cell.setCellType(Cell.CELL_TYPE_FORMULA);
cell.setCellFormula(formula);//计算公式
FormulaEvaluator evaluator = workBook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateFormulaCell(cell);
CellValue cellValue = evaluator.evaluate(cell);
return cell;
}
/**
* 统计计算公式(+)求和
* @param tagert 列标记
* @param begIndex 起始索引
* @param endIndex 终止索引
* @return 返回计算公式
*/
public static String getSumStr(String tagert,int begIndex,int endIndex){
String retStr=tagert+begIndex;
for(int i=begIndex+1;i<=endIndex;i++ ){
retStr+=("+"+tagert+i);
}
return "SUM("+retStr+")";
}
/**
* 生产excel文件到指定的路径下
* @param workBook excel对象
* @param sheetName sheet名称
* @param filePathName 保存路径
*/
public static void writeToExcelFile(HSSFWorkbook workBook,String sheetName,String filePathName){
workBook.setSheetName(0,sheetName);
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(filePathName);
} catch (FileNotFoundException e1) {
e1.printStackTrace();//异常信息需要日志记录
}
try {
workBook.write( fileOut );
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试类:
package com.jason.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.util.CellRangeAddress;
import com.jason.excel.CreateExcelUtil;
public class Test2 {
/**
* 每个报表都不相同
*/
private static String[] arr={"B","C","D","E","F","G","H","I","J","K","L"};
public static void main(String[] args)throws FileNotFoundException, IOException {
// 创建HSSFWorkbook对象
HSSFWorkbook workBook = new HSSFWorkbook();
//创建sheet对象
HSSFSheet sheet = CreateExcelUtil.getDefaultSheet(workBook,13);
//创建字体和样式
HSSFFont font=CreateExcelUtil.getFont(workBook, null, -1, -1);
HSSFCellStyle style=CreateExcelUtil.getStyle(workBook, font,"center");//居中样式
HSSFCellStyle style_left=CreateExcelUtil.getStyle(workBook,font, "left");//居左样式
//创建第1行
CreateExcelUtil.setMergeArea(sheet, 0,0,0,11);
HSSFRow row_1=CreateExcelUtil.creatRow(sheet, 0, 500);
HSSFCell cell_1_1=CreateExcelUtil.createCell(row_1, 0, style, "企业网银关键业务统计报表");
//创建第2行
CreateExcelUtil.setMergeArea(sheet, 1,1,1,3);
CreateExcelUtil.setMergeArea(sheet, 1,1,5,11);
HSSFRow row_2=CreateExcelUtil.creatRow(sheet, 1, 400);
//报表类型
HSSFCell cell_2_1=CreateExcelUtil.createCell(row_2, 0, style, "报表类型");
HSSFCell cell_2_2=CreateExcelUtil.createCell(row_2, 1, style_left,"日报表" );
//统计时间
HSSFCell cell_2_3=CreateExcelUtil.createCell(row_2, 4, style, "统计时间");
HSSFCell cell_2_4=CreateExcelUtil.createCell(row_2, 5, style_left, "2018/08/08-2018/08/09");
//创建第3行
CreateExcelUtil.setMergeArea(sheet, 2,2,1,3);
CreateExcelUtil.setMergeArea(sheet, 2,2,5,11);
HSSFRow row_3=CreateExcelUtil.creatRow(sheet, 2, -1);
//机构类型
HSSFCell cell_3_1=CreateExcelUtil.createCell(row_3, 0, style, "机构类型");
HSSFCell cell_3_2=CreateExcelUtil.createCell(row_3, 1, style_left,"分行" );
//机构名称
HSSFCell cell_3_3=CreateExcelUtil.createCell(row_3, 4, style, "机构名称");
HSSFCell cell_3_4=CreateExcelUtil.createCell(row_3, 5, style_left,"云南红塔银行总行营业部汇总" );
//创建第4行
CreateExcelUtil.setMergeArea(sheet,3,3,1,11);
HSSFRow row_4=CreateExcelUtil.creatRow(sheet, 3, -1);
//统计渠道
HSSFCell cell_4_1=CreateExcelUtil.createCell(row_4, 0, style, "统计渠道");
HSSFCell cell_4_2=CreateExcelUtil.createCell(row_4, 1, style_left,"全部" );
//创建第5行
CreateExcelUtil.setMergeArea(sheet, 4,4,0,11);
HSSFRow row_5=CreateExcelUtil.creatRow(sheet, 4, -1);
//创建第6行和第7行
CreateExcelUtil.setMergeArea(sheet, 5,6,0,1);//一级业务
CreateExcelUtil.setMergeArea(sheet, 5,6,2,3);//二级业务
CreateExcelUtil.setMergeArea(sheet, 5,5,4,5);//成功数
CreateExcelUtil.setMergeArea(sheet, 5,5,6,7);//失败数
CreateExcelUtil.setMergeArea(sheet, 5,5,8,9);//成功金额
CreateExcelUtil.setMergeArea(sheet, 5,5,10,11);//失败金额
HSSFRow row_6=CreateExcelUtil.creatRow(sheet, 5, -1);
HSSFRow row_7=CreateExcelUtil.creatRow(sheet, 6, -1);
//一级业务
HSSFCell cell_6_1=CreateExcelUtil.createCell(row_6, 0, style, "一级业务");
//二级业务
HSSFCell cell_6_2=CreateExcelUtil.createCell(row_6, 2, style, "二级业务");
//成功数
HSSFCell cell_6_3=CreateExcelUtil.createCell(row_6, 4, style, "成功数");
//单位个人
HSSFCell cell_7_1=CreateExcelUtil.createCell(row_7, 4, style, "单位");
HSSFCell cell_7_2=CreateExcelUtil.createCell(row_7, 5, style, "个人");
//失败数
HSSFCell cell_6_4=CreateExcelUtil.createCell(row_6, 6, style, "失败数");
//单位个人
HSSFCell cell_7_3=CreateExcelUtil.createCell(row_7, 6, style, "单位");
HSSFCell cell_7_4=CreateExcelUtil.createCell(row_7, 7, style, "个人");
//成功金额
HSSFCell cell_6_5=CreateExcelUtil.createCell(row_6, 8, style, "成功金额");
//单位个人
HSSFCell cell_7_5=CreateExcelUtil.createCell(row_7, 8, style, "单位");
HSSFCell cell_7_6=CreateExcelUtil.createCell(row_7, 9, style, "个人");
//失败金额
HSSFCell cell_6_6=CreateExcelUtil.createCell(row_6, 10, style, "失败金额");
//单位个人
HSSFCell cell_7_7=CreateExcelUtil.createCell(row_7, 10, style, "单位");
HSSFCell cell_7_8=CreateExcelUtil.createCell(row_7, 11, style, "个人");
//循环处理数据
int rowIndex=7;//从第7行开始计算
int dataCount=7;
for(int i=0;i<10;i++,rowIndex++){//循环产生行数据
dataCount++;
//创建新的行
HSSFRow row_index=CreateExcelUtil.creatRow(sheet, rowIndex, -1);
HSSFCell cell_1=CreateExcelUtil.createCell(row_index, 0, style, "世博支行");
for(int j=1;j<12;j++){//循环产生列数据
HSSFCell cell=CreateExcelUtil.createCell(row_index, j, style, "100");
}
}
//合计统计
HSSFRow row_total=CreateExcelUtil.creatRow(sheet, rowIndex, -1);
HSSFCell cell_total=CreateExcelUtil.createCell(row_total, 0, style, "合计");
for(int i=0;i<arr.length;i++){
String formula=CreateExcelUtil.getSumStr(arr[i],8,dataCount);
HSSFCell cell=CreateExcelUtil.createCellFormula(workBook,row_total, i+1, style, null,formula);
}
//开始写入文件: 定义你需要的输出流
CreateExcelUtil.writeToExcelFile(workBook, "企业网银关键业务统计报表", "./test.xls");
}
}
生成结果图:

未封装的代码:
package com.jason.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.util.CellRangeAddress;
public class Test {
/**
* 每个报表都不相同
*/
private static String[] arr={"B","C","D","E","F","G","H","I","J","K","L"};
public static void main(String[] args)throws FileNotFoundException, IOException {
// 创建HSSFWorkbook对象
HSSFWorkbook workBook = new HSSFWorkbook();
//创建sheet对象
HSSFSheet sheet = workBook.createSheet();
//设置默认列宽
sheet.setDefaultColumnWidth(13);
//创建字体和样式
HSSFFont font=workBook.createFont();
HSSFCellStyle style=workBook.createCellStyle();//居中样式
HSSFCellStyle style_left=workBook.createCellStyle();
/**
* 设置边框样式
* style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
* style.setBottomBorderColor(HSSFColor.BLACK.index);
* style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
* style.setLeftBorderColor(HSSFColor.BLACK.index);
* style.setBorderRight(HSSFCellStyle.BORDER_THIN);
* style.setRightBorderColor(HSSFColor.BLACK.index);
* style.setBorderTop(HSSFCellStyle.BORDER_THIN);
* style.setTopBorderColor(HSSFColor.BLACK.index);
*/
/**
* 设置背景色样式
* style.setFillForegroundColor(HSSFColor.LEMON_CHIFFON.index);
* style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//单元格模式[SOLID_FOREGROUND]
*/
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置字体居中(OK)
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
style_left.setVerticalAlignment(HSSFCellStyle.ALIGN_LEFT);//上下居左
font.setFontName("宋体");
font.setFontHeightInPoints((short) 14);// 设置字体大小
style.setFont(font);//设置字体样式
style_left.setFont(font);//设置字体样式
//创建第1行
sheet.addMergedRegion(new CellRangeAddress(0,0,0,11));
HSSFRow row_1=sheet.createRow(0);
//设置行高
row_1.setHeight((short)500);
HSSFCell cell_1_1=row_1.createCell(0);
cell_1_1.setCellStyle(style);
cell_1_1.setCellValue("企业网银关键业务统计报表");
//创建第2行
sheet.addMergedRegion(new CellRangeAddress(1,1,1,3));
sheet.addMergedRegion(new CellRangeAddress(1,1,5,11));
HSSFRow row_2=sheet.createRow(1);
row_2.setHeight((short)400);
//报表类型
HSSFCell cell_2_1=row_2.createCell(0);
cell_2_1.setCellStyle(style);
cell_2_1.setCellValue("报表类型");
HSSFCell cell_2_2=row_2.createCell(1);
cell_2_2.setCellStyle(style_left);
cell_2_2.setCellValue("日报表");
//统计时间
HSSFCell cell_2_3=row_2.createCell(4);
cell_2_3.setCellStyle(style);
cell_2_3.setCellValue("统计时间");
HSSFCell cell_2_4=row_2.createCell(5);
cell_2_4.setCellStyle(style_left);
cell_2_4.setCellValue("2018/08/08-2018/08/09");
//创建第3行
sheet.addMergedRegion(new CellRangeAddress(2,2,1,3));
sheet.addMergedRegion(new CellRangeAddress(2,2,5,11));
HSSFRow row_3=sheet.createRow(2);
//机构类型
HSSFCell cell_3_1=row_3.createCell(0);
cell_3_1.setCellStyle(style);
cell_3_1.setCellValue("机构类型");
HSSFCell cell_3_2=row_3.createCell(1);
cell_3_2.setCellStyle(style_left);
cell_3_2.setCellValue("分行");
//机构名称
HSSFCell cell_3_3=row_3.createCell(4);
cell_3_3.setCellStyle(style);
cell_3_3.setCellValue("机构名称");
HSSFCell cell_3_4=row_3.createCell(5);
cell_3_4.setCellStyle(style_left);
cell_3_4.setCellValue("云南红塔银行总行营业部汇总");
//创建第4行
sheet.addMergedRegion(new CellRangeAddress(3,3,1,11));
HSSFRow row_4=sheet.createRow(3);
//统计渠道
HSSFCell cell_4_1=row_4.createCell(0);
cell_4_1.setCellStyle(style);
cell_4_1.setCellValue("统计渠道");
HSSFCell cell_4_2=row_4.createCell(1);
cell_4_2.setCellStyle(style_left);
cell_4_2.setCellValue("全部");
//创建第5行
sheet.addMergedRegion(new CellRangeAddress(4,4,0,11));
HSSFRow row_5=sheet.createRow(4);
//创建第6行和第7行
sheet.addMergedRegion(new CellRangeAddress(5,6,0,1));//一级业务
sheet.addMergedRegion(new CellRangeAddress(5,6,2,3));//二级业务
sheet.addMergedRegion(new CellRangeAddress(5,5,4,5));//成功数
sheet.addMergedRegion(new CellRangeAddress(5,5,6,7));//失败数
sheet.addMergedRegion(new CellRangeAddress(5,5,8,9));//成功金额
sheet.addMergedRegion(new CellRangeAddress(5,5,10,11));//失败金额
HSSFRow row_6=sheet.createRow(5);
HSSFRow row_7=sheet.createRow(6);
//一级业务
HSSFCell cell_6_1=row_6.createCell(0);
cell_6_1.setCellStyle(style);
cell_6_1.setCellValue("一级业务");
//二级业务
HSSFCell cell_6_2=row_6.createCell(2);
cell_6_2.setCellStyle(style);
cell_6_2.setCellValue("二级业务");
//成功数
HSSFCell cell_6_3=row_6.createCell(4);
cell_6_3.setCellStyle(style);
cell_6_3.setCellValue("成功数");
//单位个人
HSSFCell cell_7_1=row_7.createCell(4);
cell_7_1.setCellStyle(style);
cell_7_1.setCellValue("单位");
HSSFCell cell_7_2=row_7.createCell(5);
cell_7_2.setCellStyle(style);
cell_7_2.setCellValue("个人");
//失败数
HSSFCell cell_6_4=row_6.createCell(6);
cell_6_4.setCellStyle(style);
cell_6_4.setCellValue("失败数");
//单位个人
HSSFCell cell_7_3=row_7.createCell(6);
cell_7_3.setCellStyle(style);
cell_7_3.setCellValue("单位");
HSSFCell cell_7_4=row_7.createCell(7);
cell_7_4.setCellStyle(style);
cell_7_4.setCellValue("个人");
//成功金额
HSSFCell cell_6_5=row_6.createCell(8);
cell_6_5.setCellStyle(style);
cell_6_5.setCellValue("成功金额");
//单位个人
HSSFCell cell_7_5=row_7.createCell(8);
cell_7_5.setCellStyle(style);
cell_7_5.setCellValue("单位");
HSSFCell cell_7_6=row_7.createCell(9);
cell_7_6.setCellStyle(style);
cell_7_6.setCellValue("个人");
//失败金额
HSSFCell cell_6_6=row_6.createCell(10);
cell_6_6.setCellStyle(style);
cell_6_6.setCellValue("失败金额");
//单位个人
HSSFCell cell_7_7=row_7.createCell(10);
cell_7_7.setCellStyle(style);
cell_7_7.setCellValue("单位");
HSSFCell cell_7_8=row_7.createCell(11);
cell_7_8.setCellStyle(style);
cell_7_8.setCellValue("个人");
//循环处理数据
int rowIndex=7;
int dataCount=7;
for(int i=0;i<10;i++,rowIndex++){
dataCount++;
//创建新的行
HSSFRow row_index=sheet.createRow(rowIndex);
HSSFCell cell_1=row_index.createCell(0);
cell_1.setCellStyle(style);
cell_1.setCellValue("世博支行");
for(int j=1;j<12;j++){
HSSFCell cell=row_index.createCell(j);
cell.setCellStyle(style);
cell.setCellValue("100");
}
}
//合计统计
HSSFRow row_total=sheet.createRow(rowIndex);
HSSFCell cell_total=row_total.createCell(0);
cell_total.setCellStyle(style);
cell_total.setCellValue("合计");
for(int i=0;i<arr.length;i++){
HSSFCell cell=row_total.createCell(i+1);
cell.setCellStyle(style);
//开始统计
cell.setCellType(Cell.CELL_TYPE_FORMULA);
cell.setCellFormula(getSumStr(arr[i],8,dataCount));//计算公式
System.out.println(getSumStr(arr[i],8,dataCount));
//创建poi计算器
FormulaEvaluator evaluator = workBook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateFormulaCell(cell);
CellValue cellValue = evaluator.evaluate(cell);
}
//开始写入文件: 定义你需要的输出流
workBook.setSheetName(0,"企业网银关键业务统计报表");
FileOutputStream fileOut =new FileOutputStream("./test.xls");
workBook.write( fileOut );
}
/**
* 统计计算公式
* @param tagert 列标记
* @param begIndex 起始索引
* @param endIndex 终止索引
* @return 返回计算公式
*/
public static String getSumStr(String tagert,int begIndex,int endIndex){
String retStr=tagert+begIndex;
for(int i=begIndex+1;i<=endIndex;i++ ){
retStr+=("+"+tagert+i);
}
return "SUM("+retStr+")";
}
}
POI生成EXCEL文件(字体、样式、单元格合并、计算公式)的更多相关文章
- POI生成EXCEL文件
POI生成EXCEL文件 一.背景 根据指定格式的JSON文件生成对应的excel文件,需求如下 支持多sheet 支持单元格合并 支持插入图片 支持单元格样式可定制 需要 标题(title),表头( ...
- 读取Excel文件中的单元格的内容和颜色
怎样读取Excel文件中的单元格的内容和颜色 先创建一个Excel文件,在A1和A2中随意输入内容,设置A1的字体颜色为红色,A2的背景为黄色.需要 using Excel = Microsoft.O ...
- java利用poi生成excel文件后下载本地
1.该功能需要poi的jar包,链接: http://pan.baidu.com/s/1migAtNq 密码: 38fx. 2.首先新建一个实体类,用以存放单个数据 public class Test ...
- 使用poi导出Excel,并设定单元格内容类型,抛出异常
本例子使用的是HSSF,为Excel2003提供处理方案. 设定为输入类型为数值 import org.apache.poi.hssf.usermodel.DVConstraint; import o ...
- 【原创】POI 生成Excel文件并下载
ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 效果图: 实现 1.在pom中添加依赖: <!-- https://mvnrepository.com/artifact/org.apa ...
- 使用POI生成Excel文件,可以自动调整excel列宽
//autoSizeColumn()方法自动调整excel列宽 importjava.io.FileOutputStream; importorg.apache.poi.hssf.usermodel. ...
- Java 利用 poi 生成 Excel文件的通用例子
在用java 写数据库应用的时候, 通常会生成各种报表,而这些报表可能会被导出为各种格式的文件,比如Excel文档,pdf 文档等等. 今天先做了一个生成Excel 文档的例子,主要解决以下问题: 生 ...
- NPOI导出Excel文件,对单元格的一些设置
HSSFWorkbook book = new HSSFWorkbook(); MemoryStream ms = new MemoryStream(); ISheet sheet = book.Cr ...
- Jquery的一键上传组件OCUpload及POI解析Excel文件
第一步:将js文件引入页面 <script type="text/javascript" src="${pageContext.request.contextPat ...
随机推荐
- Every-SG 博弈论 mark定义和结论
http://blog.sina.com.cn/s/blog_51cea4040100h3l9.html 这种类型,可以想成这样,有N组游戏,有N个穿红色衣服的人代表先手,有N个穿蓝色衣服的人代表后手 ...
- springmvc+hibernate4事务管理配置
1.事务的特性 事务的四种特性: 原子性:体现一个事务的操作的不可分割,要么权执行,要么全不执行. 一致性:事务的执行结果必须从一种一致性状态变到另一种一致性状态.最典型的就是转账,两个账户A.B总金 ...
- Problem F: 最大公约数、最小公倍数
Description 输入两个正整数m和n,输出m.n的最大公约数和最大公倍数.先计算最大公约数,m和n得乘积除以最大公约数,就得到了最小公倍数.其中最大公约数可以用穷举法求得,也可以用辗转相除法求 ...
- iOS自定义全屏返回与tableView左划删除手势冲突解决
当自定义一个navigationController实现全屏右划返回时, 使用起来是不是很爽, 代码如下: - (void)viewDidLoad { [super viewDidLoad]; UIG ...
- OpenCV2马拉松第10圈——直方图反向投影(back project)
收入囊中 灰度图像的反向投影 彩色图像的反向投影 利用反向投影做object detect 葵花宝典 什么是反向投影?事实上没有那么高大上! 在上一篇博文学到,图像能够获得自己的灰度直方图. 反向投影 ...
- JSON数据转换到POCO的代码
转载:http://www.cnblogs.com/wintersun/archive/2012/09/14/2684708.html 在Visual Studio 2012中轻松把JSON数据转换到 ...
- Spark Client和Cluster两种运行模式的工作流程
1.client mode: In client mode, the driver is launched in the same process as the client that submits ...
- SWT常用组件
SWT类所代表的事件常量: 事件类型常量 说明 SWT.Activate 当激活窗口时 SWT.Arm 菜单项被选中之前 SWT.Close 关闭窗口时 SWT.Collapse 折叠树的节点时 SW ...
- 返回content-length=0问题解决
遇到一个奇怪问题,有时候会不显示css或图片文件,通过调试工具发现请求返回长度都是0.研究半天未果,初步猜测可能是过滤器给拦截了. 果然在一个过滤器中发现相关代码: HttpRequestWrappe ...
- LoadLibrary文件路径及windows API相关的文件路径问题
LoadLibrary HMODULE WINAPI LoadLibrary( _In_ LPCTSTR lpFileName ); Loads the specified module into ...