日期:2014-05-20  浏览次数:20773 次

java 操作excel
关于excel怎么操作比较方面,我这里要实现一个导入功能,会把我的数据导给用户。主要是excel模板(我是swing实现的,所以希望提供java代码查考)。这个模板主要就是合并比较麻烦,另外里面的有复选框等等这些,还有一个关键是中间有些内容他的行数是不固定的,如片参考:http://zhidao.baidu.com/question/319120573.html


------解决方案--------------------
有点乱 你自己整理下
封装了一个写数据的
一个调用格式撒的 原理就是基于模板修改数据

// copy附件
FileUtil.copyFile(from, to);
// 准备修改 
FileInputStream fis=new FileInputStream(to);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook wb = new HSSFWorkbook(fs);

写数据的类



import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFAnchor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;




public class AffixExcel {
/*
* 写一行分离出来 
*/
public void setRegionRow(HSSFSheet sheet,int row,List<KpiRegionValBean> urkl,String kpiname,HSSFCellStyle cs,HSSFCellStyle cs1){
if (row == 2) {
HSSFRow row1 = sheet.getRow(1);
if(row1==null)
row1 = sheet.createRow(1);
for (int ni = 0; ni < urkl.size(); ni++) {
HSSFCell c1 = row1.getCell(ni + 1);
if (c1 == null)
c1 = row1.createCell(ni + 1);
c1.setCellStyle(cs1);
c1.setCellType(HSSFCell.CELL_TYPE_STRING);
c1.setCellValue(urkl.get(ni).getRegion_name());
}
}

HSSFRow row2 = sheet.createRow(row);
if(row2==null)
row2 = sheet.createRow(row);
HSSFCell c = row2.getCell(0);
if (c == null)
c = row2.createCell(0);
c.setCellStyle(cs1);
c.setCellType(HSSFCell.CELL_TYPE_STRING);
c.setCellValue(kpiname);
for (int mi = 0; mi < urkl.size(); mi++) {
c = row2.getCell(mi + 1);
if (c == null)
c = row2.createCell(mi + 1);
c.setCellStyle(cs);
c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue(Double.valueOf(urkl.get(mi).getVal()));
}
}
/*
* 历史数据写入
*/
public void setDateRow(HSSFSheet sheet,int rowi,List<kpiDateValBean> urkd,String kpiname,HSSFCellStyle cs,HSSFCellStyle cs1){
if (rowi == 2) {
HSSFRow row = sheet.getRow(1);
if(row==null)
row = sheet.createRow(1);
for (int ni = 0; ni < urkd.size(); ni++) {
HSSFCell c = row.getCell(ni + 1);
if (c == null)
c = row.createCell(ni + 1);
c.setCellStyle(cs1);
c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue(urkd.get(ni).getDate_key()
.substring(
urkd.get(ni).getDate_key()
.length() - 2));
}
}

HSSFRow row2 = sheet.createRow(rowi);
if(row2==null)
row2 = sheet.createRow(rowi);
HSSFCell c1 = row2.getCell(0);
if (c1 == null)
c1 = row2.createCell(0);
c1.setCellStyle(cs1);
c1.setCellType(HSSFCell.CELL_TYPE_STRING);
c1.setCellValue(kpiname);
for (int mi = 0; mi < urkd.size(); mi++) {
c1 = row2.getCell(mi + 1);
if (c1 == null)
c1 = row2.createCell(mi + 1);
c1.setCellStyle(cs);
c1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
c1.setCellValue(Double.valueOf(urkd.get(mi).getVal()));
}
}

/*
* 写第一行的时间
*/
public void setTime(HSSFSheet sheet,String time,HSSFCellStyle cs){
HSSFRow row = sheet.getRow(0);
if(row==null)
row = sheet.createRow(0);
HSSFCell cell = row.getCell(0);
if (cell == null)
cell = row.createCell(0);
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell.setCellStyle(cs);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(time);
}

/*
* 写第一行的名称