JAVA 创建excel(xlsx)POI技术

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import org.apache.poi.xssf.usermodel.XSSFFont;

import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.beans.*;

import com.dao.*;



public String getPop() {


PopDao dao = new PopDao ();

List<Proposal> ProposalList = dao.query().results();
int size = ProposalList.size();
String result = null;

String[] names = new String[] { "id", "ID", "发布时间", "建议内容", "建议类型", "操作" };
XSSFWorkbook workBook = new XSSFWorkbook(); // 创建Excel文档
XSSFSheet sheet = workBook.createSheet("数据报告");
XSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
XSSFCell[] firstcell = new XSSFCell[names.length];
XSSFCellStyle style = workBook.createCellStyle();
XSSFFont font = workBook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
style.setFont(font);

for (int j = 0; j < names.length; j++) {//首行
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellStyle(style);
firstcell[j].setCellValue(new XSSFRichTextString(names[j]));
}

if (size > 0) {
for (int i = 0; i < size; i++) {
// 创建一行
XSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一条记录
Proposal proposal = ProposalList.get(i);
for (int colu = 0; colu < names.length - 1; colu++) {
XSSFCell id = row.createCell(0);// 1
id.setCellValue(proposal.getId());

XSSFCell ID = row.createCell(1);// 2
ID.setCellValue(proposal.getID());

XSSFCell dateTime = row.createCell(2);// 3
dateTime.setCellValue(proposal.getDateTime());

XSSFCell Content = row.createCell(3);// 4
Content.setCellValue(proposal.getContent());

XSSFCell State = row.createCell(4);// 5
State.setCellValue(proposal.getState());

}
}
}
try {
// 创建文件输出流,准备输出电子表格
OutputStream out = new FileOutputStream(C:\Users\Lollipop\Desktop\excel\Proposals(用户反馈信息表).xlsx");
workBook.write(out);
out.close();
} catch (Exception e) {
result = "fail";
return result;
}
result = "success";
return result;

}



需要jar包:

poi-3.8-20120326.jar

poi-ooxml-3.8-20120326.jar

dom4j-1.6.1.jar

poi-ooxml-schemas-3.8-20120326.jar

xmlbeans-2.3.0.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值