您的位置:首页 > 运维架构 > Apache

Apache POI导出Excel文件

2010-07-22 13:38 288 查看
在实际开发过程中,经常会遇到将查询数据导出到Excel的需求,为了方便操作,自己特意编写了一个比较通用的工具类ExcelUtils

现提供代码如下,希望对大家有所帮助:

import org.apache.poi.hssf.usermodel.HSSFCell;
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.Region;
import org.apache.poi.ss.usermodel.CellStyle;


public class ExcelUtils {
//生成Excel表头
public static void exportHeaders(HSSFWorkbook workbook,HSSFSheet sheet,String headers){
/*表头行以;分割
*表头项目以:分割
*表头项目项以,分割
*/
//设置单元格样式
CellStyle style=workbook.createCellStyle();
//设置水平居中对齐
style.setAlignment((short)(CellStyle.ALIGN_CENTER));
//设置垂直居中对齐
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
//设置自动换行
style.setWrapText(true);

String[] Rows=headers.split(";");
for(int m=0;m<Rows.length;m++){
HSSFRow hssfRow=sheet.createRow(m);
String Row=Rows[m];
String[] HeadItems=Row.split(":");
for(int n=0;n<HeadItems.length;n++){
String HeadItem=HeadItems
;
String[] Items=HeadItem.split(",");
short fromRow=(short)m;
short fromCol=(short)(Short.parseShort(Items[0])-1);
String headContent;
if(Items.length==4){
short toRow=(short)(Short.parseShort(Items[1])-1);
short toCol=(short)(Short.parseShort(Items[2])-1);
headContent=Items[3];
Region region=new Region(fromRow,fromCol,toRow,toCol);
sheet.addMergedRegion(region);
}else{
headContent=Items[1];
}
HSSFCell cell=hssfRow.createCell(fromCol);
cell.setCellValue(headContent);
cell.setCellStyle(style);
}
}
}

public static void exportRow(HSSFSheet sheet,int rowIndex,String[] datas){
HSSFRow row=sheet.createRow(rowIndex);
for(int i=0;i<datas.length;i++){
HSSFCell cell=row.createCell(i);
cell.setCellValue(datas[i]);
}
}
}


示例如下:

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
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.Region;


public class Demo {

public static void main(String[] args) throws Exception{
FileOutputStream out=new FileOutputStream("D:/集团公司级管控客户收入统计表.xls");
HSSFWorkbook workbook=new HSSFWorkbook();
HSSFSheet sheet=workbook.createSheet();
String headers="";
headers+="1,1,32,2010年集团公司级管控客户收入统计表;";
headers+="1,5,1,序号:2,5,2,客户编码:3,5,3,中国总部所在地:4,4,5,第一营销服务责任部门:6,5,6,客户名称:7,5,7,销售组织:8,5,8,分类:9,5,9,客户级别:10,2,14,固话语音收入(含小灵通):15,2,17,移动语音收入:18,2,23,互联网及数据业务收入:24,2,32,增值及综合信息应用服务;";
headers+="10,5,10,本地通信:11,3,13,长途通话:14,5,14,固网语音其他收入:15,5,15,月租费:16,5,16,通话费:17,5,17,其他收入:18,3,19,互联网接入:20,3,23,资源出租:24,4,25,增值业务收入:26,4,31,IT服务及应用:32,5,32,其他收入;";
headers+="11,5,11,国内长途通话费:12,5,12,国际长途通话费:13,5,13,港澳台电话通话费:18,5,18,宽带接入:19,5,19,其他互联网接入:20,5,20,出租电路:21,5,21,移动资源出租:22,5,22,出租设备:23,5,23,其他出租收入;";
headers+="4,国内带宽型业务第一责任方:5,国台带宽型业务第一责任方:24,固网增值业务:25,移动增值业务:26,集成服务:27,外包及专业服务:28,信息化应用:29,知识服务:30,软件开发及销售:31,其他IT服务及应用";
ExcelUtils.exportHeaders(workbook,sheet, headers);
workbook.write(out);
out.close();
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: