关于_java程序生成Excel文件
package com.demo;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.entity.ExcelData;
public class ExportExcel {
private String idex;
private String node_label;
private String lerep;
private String industryphy;
private String ahchy;
private String regcap;
private String estdate;
private String state;
private String opscope;
private String label;
private String relation;
private InputStream fileInputStream;
private String name;
public static void main(String[] args) {
new ExportExcel().getExcel();
}
public static List<ExcelData> getList(){
List<ExcelData> list = new ArrayList<ExcelData>();
ExcelData ex =null;
for (int i = 0; i < 10; i++) {
ex=new ExcelData();
ex.setIdex(""+i);
ex.setState("正常");
ex.setTo_node_label("安徽省XXXXXX");
ex.setAhchy("金融");
ex.setMarprid("合肥");
list.add(ex);
}
return list;
}
public void getExcel(){
List<ExcelData> list=getList(); //获取对象的列表
int numIndexFirst=0;//二维数组行数
int numIndexSecond=11;//二维数组列数
HSSFWorkbook wb = new HSSFWorkbook();//通过企业名称获取表格数据
HSSFSheet sheet = wb.createSheet("股东信息列表");
sheet.setDefaultColumnWidth((short)16);//设置默认宽度为16
sheet.setColumnWidth(8,40*256);
HSSFCellStyle style=wb.createCellStyle();//设置样式
style.setWrapText(true);//自动换行
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//字体居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
if(list.size()>0){numIndexFirst=list.size()+1;
}else{ numIndexFirst=1; }
Object[][] datas=new Object[numIndexFirst][numIndexSecond];//装填数据标题头
datas[0][0]="序号";
datas[0][1]="企业名称";
datas[0][2]="法人代表";
datas[0][3]="国标行业";
datas[0][4]="";
datas[0][5]="注册资本(万元)";
datas[0][6]="成立日期";
datas[0][7]="登记状态";
datas[0][8]="经营范围";
datas[0][9]="";
datas[0][10]="";
for (int i = 1; i < numIndexFirst; i++) {//根据查询数据继续装填数组
for (int j = 0; j < numIndexSecond; j++) {
if(j==0){
idex=list.get(i-1).getIdex()==null?"":list.get(i-1).getIdex();
datas[i][j]=idex;
}else if(j==1){
node_label=list.get(i-1).getTo_node_label()==null?"":list.get(i-1).getTo_node_label();
datas[i][j]=node_label;
}else if(j==2){
lerep=list.get(i-1).getLerep()==null?"":list.get(i-1).getLerep();
datas[i][j]=lerep;
}else if(j==3){
industryphy=list.get(i-1).getIndustryphy()==null?"":list.get(i-1).getIndustryphy();
datas[i][j]=industryphy;
}else if(j==4){
ahchy=list.get(i-1).getAhchy()==null?"":list.get(i-1).getAhchy();
datas[i][j]=ahchy;
}else if(j==5){
regcap=list.get(i-1).getRegcap()==null?"":list.get(i-1).getRegcap();
datas[i][j]=regcap;
}else if(j==6){
estdate=list.get(i-1).getEstdate()==null?"":list.get(i-1).getEstdate();
datas[i][j]=estdate;
}else if(j==7){
state=list.get(i-1).getState()==null?"":list.get(i-1).getState();
datas[i][j]=state;
}else if(j==8){
opscope=list.get(i-1).getOpscope()==null?"":list.get(i-1).getOpscope();
datas[i][j]=opscope;
}else if(j==9){
label=list.get(i-1).getLabel()==null?"":list.get(i-1).getLabel();
datas[i][j]=label;
}else if(j==10){
relation=list.get(i-1).getRelation()==null?"":list.get(i-1).getRelation();
datas[i][j]=relation;
}
}
}
//将二维数组中数据放入到excel表格中
HSSFRow row;
HSSFCell cell;
for(int i = 0; i < datas.length; i++) {
row = sheet.createRow(i);//创建表格行
for(int j = 0; j < datas[i].length; j++) {
cell = row.createCell(j);//根据表格行创建单元格
cell.setCellValue(String.valueOf(datas[i][j]));
cell.setCellStyle(style);
}
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
fileInputStream = null;
FileOutputStream bos = null;
try {
fileInputStream = new BufferedInputStream(is);
bos = new FileOutputStream( new File("U:/mess/demo.xls"));
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = fileInputStream.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null)
//fileInputStream.close();
if (bos != null)
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("文件已生成,请查看");
// wb.write(new FileOutputStream("U:/demo.xls"));
}
public String getIdex() {
return idex;
}
public void setIdex(String idex) {
this.idex = idex;
}
public String getNode_label() {
return node_label;
}
public void setNode_label(String node_label) {
this.node_label = node_label;
}
public String getLerep() {
return lerep;
}
public void setLerep(String lerep) {
this.lerep = lerep;
}
public String getIndustryphy() {
return industryphy;
}
public void setIndustryphy(String industryphy) {
this.industryphy = industryphy;
}
public String getAhchy() {
return ahchy;
}
public void setAhchy(String ahchy) {
this.ahchy = ahchy;
}
public String getRegcap() {
return regcap;
}
public void setRegcap(String regcap) {
this.regcap = regcap;
}
public String getEstdate() {
return estdate;
}
public void setEstdate(String estdate) {
this.estdate = estdate;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getOpscope() {
return opscope;
}
public void setOpscope(String opscope) {
this.opscope = opscope;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getRelation() {
return relation;
}
public void setRelation(String relation) {
this.relation = relation;
}
public InputStream getFileInputStream() {
return fileInputStream;
}
public void setFileInputStream(InputStream fileInputStream) {
this.fileInputStream = fileInputStream;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}