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