本文主要是介绍java中使用POI(HSSF)导出excel表格,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用POI(HSSF)导出excel表格,具体代码如下:
public void exportUserExcel(@Param("activityId") long activityId) {try {List<string> list =new ArrayList<string>;//list为自己从数据库查出的数据response.setContentType("application/xls;charset=utf-8");String file_name = new String("用户统计".getBytes("utf-8"), "iso8859-1");response.setHeader("Content-disposition", "attachment; filename=" + file_name + ".xls");HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = null;HSSFRow rowContent = null;String[] titles = { "第一列", "第二列", "第三列", "第四列" };int sheetFlag = 0, rowFlag = 0;if (list.size() > 0) {//设置第一行列名for (int i = 0, len = list.size(); i < len; i++) {if (i % 10000 == 0) {sheetFlag++;sheet = wb.createSheet("Sheet" + sheetFlag);HSSFRow row = sheet.createRow(0);for (int j = 0; j < titles.length; j++) {Cell cell = row.createCell(j);cell.setCellValue(titles[j]);sheet.setColumnWidth(j, 5000);//设置单元格宽度}rowFlag = 0;}rowFlag++;rowContent = sheet.createRow(rowFlag);MtcRssActivityList rssActivity = list.get(i);HSSFCell cell0 = rowContent.createCell(0);cell0.setCellValue(rowFlag+"行1列");HSSFCell cell1 = rowContent.createCell(1);cell1.setCellValue(rowFlag+"行2列");HSSFCell cell2 = rowContent.createCell(2);cell2.setCellValue(rowFlag+"行3列");HSSFCell cell3 = rowContent.createCell(3);cell3.setCellValue(rowFlag+"行4列");//........}} else {sheet = wb.createSheet("Sheet1");}OutputStream bos = response.getOutputStream();wb.write(bos);bos.close();} catch (Exception e) {}}
这篇关于java中使用POI(HSSF)导出excel表格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!