Java实现Excel编辑之POI


最后编辑时间:2015-02-2817:54

JAVA通常有两种方法来操作Excel,分别是POI和JExcelAPI,而且都是开源的。

POI参考:

JExcelAPI参考:

http://www.oschina.net/code/snippet_126103_3553

http://blog.csdn.net/aking21alinjuju/article/details/5996110

应用场景:

* excel的第一个sheet首行添加标题,宋体,18号,加粗,在内容所有列合并居中;

* 在最后添加一行,格式与初始第一行一样,字体颜色为蓝色;

* A列单元格统计记录数,GH列单元格使用公式统计数值,货币格式呈现;

POI使用JAR:poi-3.8-20120326.jar, poi-ooxml-3.8-20120326.jar , poi-ooxml-schemas-3.8-20120326.jar ,xmlbeans-2.3.jar

代码:

package charlie.utils.excel;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import org.apache.poi.hssf.util.CellRangeAddress;

import org.apache.poi.hssf.util.HSSFColor;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.CellStyle;

import org.apache.poi.ss.usermodel.Font;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.ss.usermodel.WorkbookFactory;

/**

* @author CharlieChen

* @DateTime 2015-1-5 上午11:46:43

* @version 1.0

*/

@SuppressWarnings("deprecation")

public class PoiUtil {

/**

* @param args

*/

public static void main(String[] args) {

PoiUtil poiUtil = new PoiUtil();

String excelPath = "D:/temp/test/abc.xlsx";

Workbook wb = poiUtil.getExcelWorkBook(excelPath);

String reName = "D:/temp/test/temp.xlsx";

poiUtil.editExcel(wb,reName);

}

/**

* excel的第一个sheet首行添加标题,宋体,18号,加粗,在内容所有列合并居中

* 在最后添加一行,格式与初始第一行一样,字体颜色为蓝色,

* A列单元格统计记录数,GH列单元格使用公式统计数值,货币形式呈现

* @param wb

* @param reName

*/

public void editExcel(Workbook wb,String reName){

Sheet sheet = wb.getSheetAt(0);

int maxRow = sheet.getLastRowNum();//获取行数

Row firstRow = sheet.getRow(0);//获取第一行

int maxColumn = firstRow.getLastCellNum();//获取列数

sheet.shiftRows(0, maxRow, 1);//整体下移一行,相当于在首行插入一行

Row titleRow = sheet.createRow(0);//新增行

//增加合并区域,首行从第一列到有内容的最后一列

sheet.addMergedRegion(new CellRangeAddress(0,0,0,maxColumn-1));

//合并单元格

Cell titleCell = titleRow.createCell(0);

CellStyle cellStyle = wb.createCellStyle();

titleCell.setCellValue("显示内容");

Font font = wb.createFont();

font.setFontName("宋体");//字体

font.setFontHeightInPoints((short)18);//字体大小

font.setBoldweight(Font.BOLDWEIGHT_BOLD);//加粗

cellStyle.setFont(font);

cellStyle.setAlignment(CellStyle.ALIGN_CENTER);//居中对齐

titleCell.setCellStyle(cellStyle);

Row lastRow = sheet.createRow(maxRow + 1);

//最后新增一行

int count = maxRow - 1;

Font lastFont = wb.createFont();

lastFont.setFontName("宋体");//字体

lastFont.setFontHeightInPoints((short)11);

//颜色设置为蓝色

lastFont.setColor(HSSFColor.BLUE.index);

CellStyle lastStyle = wb.createCellStyle();

lastStyle.cloneStyleFrom(firstRow.getCell(0).getCellStyle());

lastStyle.setFont(lastFont);

//构造最后一行

for(int i=0;i<maxColumn;i++){

lastRow.createCell(i).setCellStyle(lastStyle);

}

//填数据

lastRow.getCell(0).setCellValue("记录数:" + count);

//份额余额统计

lastRow.getCell(6).setCellType(Cell.CELL_TYPE_FORMULA);

lastRow.getCell(6).setCellFormula("sum(G3:G"+(maxRow+1)+")");

//货币格式显示

CellStyle currencyStyle = wb.createCellStyle();

currencyStyle.cloneStyleFrom(lastStyle);

currencyStyle.setDataFormat((short)4);

lastRow.getCell(6).setCellStyle(currencyStyle);

//最新市值统计

lastRow.getCell(7).setCellType(Cell.CELL_TYPE_FORMULA);

lastRow.getCell(7).setCellFormula("sum(H3:H"+(maxRow+1)+")");

lastRow.getCell(7).setCellStyle(currencyStyle);

//保存到文件

FileOutputStream is;

try {

is = new FileOutputStream(reName);

wb.write(is);

is.close();

System.out.println("保存完成");

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* @method读取0307版本的excel的公共方法,根据文件的路径创建Workbook对象

* @param filePath 文件全路径

*/

public Workbook getExcelWorkBook(String filePath) {

InputStream ins = null;

Workbook book = null;

try {

ins=new FileInputStream(new File(filePath));

book = WorkbookFactory.create(ins);

ins.close();

return book;

} catch (FileNotFoundException e1) {

e1.printStackTrace();

} catch (InvalidFormatException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (ins != null) {

try {

ins.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return null;

}

}

货币格式显示参考:

http://blog.csdn.net/zxcvbnmluton/article/details/6066916

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值