您的位置:首页 > 编程语言 > Java开发

java-poi-excel报表

2017-06-14 09:20 309 查看

java-poi-excel报表

配置

pom.xml

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.5-FINAL</version>
</dependency>


代码示例

/**
* 拆分主营占比
*/
@Override
public void splitMainProportion(HttpServletResponse response){
OutputStream os = null;
HSSFWorkbook wb = null;
List<List<String>> resultList = new ArrayList<>();
try {
resultList = getDatas();//获取数据内容
List<String> titleList = new ArrayList<>();
titleList.add("房地产主营");
titleList.add("证券代码");
titleList.add("产品名称");
titleList.add("占比");
titleList.add("图谱节点");

wb = new HSSFWorkbook();
HSSFSheet outHs = wb.createSheet("房地产主营");
createExcelTitle(wb, outHs, titleList);//创建标题
createExcelContent(wb, resultList, outHs);//创建内容
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=mainRealEstate.xls");
os = response.getOutputStream();
wb.write(os);
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if (os != null) {//不关闭资源,会导致java内存泄漏哦
os.close();
os = null;
}
}catch (Exception e){
e.printStackTrace();
}
}
}
4000

/**
* 获取数据内容
* @return
* @throws IOException
*/
private List<List<String>> getDatas() throws IOException {
List<List<String>> resultList = new ArrayList<>();
HSSFWorkbook wb = null;
wb = new HSSFWorkbook(new FileInputStream("C:\\Users\\Administrator\\Desktop\\task\\graphData\\房地产主营\\房地产主营.xls"));//创建工作簿
HSSFSheet hs = wb.getSheetAt(0);
HSSFRow row = null;
for(int i=1;i < hs.getLastRowNum();i++){
row = hs.getRow(i);
String stockCode = row.getCell(1).getStringCellValue();
String stockName = row.getCell(2).getStringCellValue();
String[] productOccupation = row.getCell(3).getStringCellValue().split(",,");
for(String str : productOccupation){
if(str.contains(":")){
String[] proOccu = str.split(":");
List<String> list = new ArrayList<>();
list.add(stockCode);
list.add(stockName);
list.add(proOccu[0]);
list.add(proOccu[1]);
list.add(ksi.getGraphByProductName(proOccu[0]));
resultList.add(list);
}
}
}
return resultList;
}

/**
* 创建excel内容
* @param wb 工作簿
* @param resultList 内容数据
* @param outHs sheet页
*/
private void createExcelContent(HSSFWorkbook wb, List<List<String>> resultList, HSSFSheet outHs) {
HSSFRow row = null;
for(int i = 0; i<resultList.size(); i++){
row = outHs.createRow(i+1);
List<String> oneContentList = resultList.get(i);
for(int j=0;j<oneContentList.size();j++){
row.createCell(j).setCellStyle(getStyle(wb));
row.getCell(j).setCellValue(oneContentList.get(j));
}
}
}

/**
* 创建excel标题
* @param wb 工作簿
* @param outHs sheel页
* @param titleList 标题集合
*/
private void createExcelTitle(HSSFWorkbook wb, HSSFSheet outHs, List<String> titleList) {
HSSFRow row = outHs.createRow(0);
for(int i=0;i<titleList.size();i++){
outHs.setColumnWidth(i, 20 * 256);//动态设置宽度
row.createCell(i).setCellStyle(getColumnTopStyle(wb));
row.getCell(i).setCellValue(titleList.get(i));
}
}

/**
* 列头单元格样式
* @param workbook
* @return
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {

// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)11);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}

/**
* 列数据信息单元格样式
* @param workbook
* @return
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
//font.setFontHeightInPoints((short)10);
//字体加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}


简单的实现了对excel文件的读和写以及涵盖了文件下载的功能

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