java导出包含多个sheet的Excel


     前面文章已经介绍了导出简单的Excel,这次小编要介绍的是如何导出含有多个sheet表的Excel。


    内容和上篇文章(http://blog.csdn.net/xiao714041/article/details/77423824)基本上是相同的,相同代码不再赘述,文章主要说关键代码。


    要导出多个sheet,关键就是Excel导出的时间设定,在执行导出文件之前,创建多个工作表

HSSFSheet sheet = workbook.createSheet(sheettitle);  
 
 

    这样每创建一个工作表,便会生成一个新的sheet表,在最后导出Excel的时候一次性导出。


    示例:

    Java类:


 
 
  1. try {
  2. HSSFWorkbook workbook = new HSSFWorkbook();
  3. OutputStream out = response.getOutputStream();
  4. for( int j= 0;j<n;j++){
  5. BaseResult<List<T>> teasalList = service.select(teasal);
  6. //接下来循环list放到Excel表中
  7. if(teasalList.isSuccess()&&teasalList.getResult().size()> 0){
  8. //文件标题
  9. SimpleDateFormat formatter1 = new SimpleDateFormat( "yyyy-MM-dd");
  10. String nowdate = formatter1.format( new Date());
  11. String title = null;
  12. title = "excel表格标题-" + nowdate + ".xls";
  13. String sheettitle = "sheet表名";
  14. //设置表格标题行
  15. String oneheaders = "首行标题" ;
  16. String dateheaders = nowdate ;
  17. String[] headers = new String[] { "列1", "列2", "列3", "列4"};
  18. List<Object[]> dataList = new ArrayList<Object[]>();
  19. Object[] objs = null;
  20. for( int i = 0; i< 3 ; i++){ //循环每一条数据
  21. objs = new Object[headers.length];
  22. objs[ 1] = "张三"; //姓名
  23. objs[ 2] = "3"; //序号
  24. //数据添加到excel表格
  25. dataList.add(objs);
  26. }
  27. //使用流将数据导出
  28. //防止中文乱码
  29. String headStr = "attachment; filename=\"" + new String( title.getBytes( "gb2312"), "ISO8859-1" ) + "\"";
  30. response.setContentType( "octets/stream");
  31. response.setContentType( "APPLICATION/OCTET-STREAM");
  32. response.setHeader( "Content-Disposition", headStr);
  33. ExportExcelDownFee ex ;
  34. ex = new ExportExcelDownFee(sheettitle, oneheaders, dateheaders,headers, dataList); //没有标题
  35. ex.export(workbook,out);
  36. }
  37. }
  38. workbook.write(out); //循环生成多个sheet之后在导出Excel
  39. out.close(); //关闭流
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }



    工具类:


 
 
  1. public class ExportExcelDownFee {
  2. //导出表的列名
  3. private String[] rowName ;
  4. //导出表的小标题
  5. private String oneheaders;
  6. //导出表的日期
  7. private String dateheaders;
  8. //sheet表表名
  9. private String sheettitle;
  10. private List<Object[]> dataList = new ArrayList<Object[]>();
  11. HttpServletResponse response;
  12. //构造方法2,传入要导出的数据
  13. public ExportExcelDownFee( String sheettitle, String oneheaders, String dateheaders, String[] rowName,List<Object[]> dataList){
  14. this.dataList = dataList;
  15. this.oneheaders = oneheaders;
  16. this.dateheaders = dateheaders;
  17. this.rowName = rowName;
  18. this.sheettitle = sheettitle;
  19. }
  20. /*
  21. * 导出数据
  22. * */
  23. public void export(HSSFWorkbook workbook,OutputStream out) throws Exception{
  24. try{
  25. HSSFSheet sheet = workbook.createSheet(sheettitle); // 创建工作表
  26. HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook); //获取列头样式对象
  27. HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象
  28. //第一行
  29. HSSFRow rowfirstName = sheet.createRow( 0);
  30. HSSFCell oneCellRowName = rowfirstName.createCell( 0); //创建列头对应个数的单元格
  31. oneCellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型
  32. HSSFRichTextString onetext = new HSSFRichTextString(oneheaders);
  33. oneCellRowName.setCellValue(onetext); //设置列头单元格的值
  34. //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
  35. sheet.addMergedRegion( new CellRangeAddress( 0, 0, 0, 3));
  36. oneCellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式
  37. //第二行
  38. HSSFRow rowDateName = sheet.createRow( 1);
  39. HSSFCell DateCellRowName = rowDateName.createCell( 3);
  40. DateCellRowName.setCellValue(dateheaders);
  41. DateCellRowName.setCellStyle(columnTopStyle);
  42. // 定义所需列数
  43. int columnNum = rowName.length;
  44. HSSFRow rowRowName = sheet.createRow( 2); // 在索引2的位置创建行(最顶端的行开始的第二行)
  45. // 将列头设置到sheet的单元格中
  46. for( int n= 0;n<columnNum;n++){
  47. HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格
  48. cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型
  49. HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
  50. cellRowName.setCellValue(text); //设置列头单元格的值
  51. cellRowName.setCellStyle(style); //设置列头单元格样式
  52. }
  53. //将查询出的数据设置到sheet对应的单元格中
  54. for( int i= 0;i<dataList.size();i++){
  55. Object[] obj = dataList.get(i); //遍历每个对象
  56. HSSFRow row = sheet.createRow(i+ 3); //创建所需的行数(从第二行开始写数据)
  57. for( int j= 0; j<obj.length; j++){
  58. HSSFCell cell = null; //设置单元格的数据类型
  59. if(j == 0){
  60. cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);
  61. cell.setCellValue(i+ 1);
  62. } else{
  63. cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
  64. if(! "".equals(obj[j]) && obj[j] != null){
  65. cell.setCellValue(obj[j].toString()); //设置单元格的值
  66. }
  67. }
  68. cell.setCellStyle(style); //设置单元格样式
  69. }
  70. }
  71. //让列宽随着导出的列长自动适应
  72. for ( int colNum = 0; colNum < columnNum; colNum++) {
  73. int columnWidth = sheet.getColumnWidth(colNum) / 256;
  74. for ( int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
  75. HSSFRow currentRow;
  76. //当前行未被使用过
  77. if (sheet.getRow(rowNum) == null) {
  78. currentRow = sheet.createRow(rowNum);
  79. } else {
  80. currentRow = sheet.getRow(rowNum);
  81. }
  82. if (currentRow.getCell(colNum) != null) {
  83. HSSFCell currentCell = currentRow.getCell(colNum);
  84. if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
  85. int length = 0;
  86. try {
  87. length = currentCell.getStringCellValue().getBytes().length;
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91. if (columnWidth < length) {
  92. columnWidth = length;
  93. }
  94. }
  95. }
  96. }
  97. if(colNum == 0){
  98. sheet.setColumnWidth(colNum, (columnWidth- 2) * 256);
  99. } else{
  100. sheet.setColumnWidth(colNum, (columnWidth+ 4) * 256);
  101. }
  102. }
  103. } catch(Exception e){
  104. e.printStackTrace();
  105. }
  106. }
  107. /*
  108. * 列头单元格样式
  109. */
  110. public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
  111. // 设置字体
  112. HSSFFont font = workbook.createFont();
  113. //设置字体大小
  114. font.setFontHeightInPoints(( short) 11);
  115. //字体加粗
  116. //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  117. //设置字体名字
  118. font.setFontName( "宋体");
  119. //设置样式;
  120. HSSFCellStyle style = workbook.createCellStyle();
  121. //在样式用应用设置的字体;
  122. style.setFont(font);
  123. //设置自动换行;
  124. style.setWrapText( false);
  125. //设置水平对齐的样式为居中对齐;
  126. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  127. //设置垂直对齐的样式为居中对齐;
  128. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  129. return style;
  130. }
  131. /*
  132. * 列数据信息单元格样式
  133. */
  134. public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
  135. // 设置字体
  136. HSSFFont font = workbook.createFont();
  137. //设置字体名字
  138. font.setFontName( "宋体");
  139. //设置样式;
  140. HSSFCellStyle style = workbook.createCellStyle();
  141. //设置底边框;
  142. style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  143. //设置底边框颜色;
  144. style.setBottomBorderColor(HSSFColor.BLACK.index);
  145. //设置左边框;
  146. style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  147. //设置左边框颜色;
  148. style.setLeftBorderColor(HSSFColor.BLACK.index);
  149. //设置右边框;
  150. style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  151. //设置右边框颜色;
  152. style.setRightBorderColor(HSSFColor.BLACK.index);
  153. //设置顶边框;
  154. style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  155. //设置顶边框颜色;
  156. style.setTopBorderColor(HSSFColor.BLACK.index);
  157. //在样式用应用设置的字体;
  158. style.setFont(font);
  159. //设置自动换行;
  160. style.setWrapText( false);
  161. //设置水平对齐的样式为居中对齐;
  162. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  163. //设置垂直对齐的样式为居中对齐;
  164. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  165. return style;
  166. }
  167. }








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值