2 cherrx cherrx 于 2017.09.13 17:07 提问

XSSF导出EXCEL表格无法让列宽自适应

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("扣费明细");

   XSSFCellStyle cellStyle = workbook.createCellStyle();
   cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
   cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
   cellStyle.setWrapText(false);
   cellStyle.setBorderBottom(BorderStyle.THIN);    
   cellStyle.setBorderLeft(BorderStyle.THIN);   
   cellStyle.setBorderTop(BorderStyle.THIN);    
   cellStyle.setBorderRight(BorderStyle.THIN); 
   int a,b,c,d;
   for(int i =0;i <=list.size();i++){
        a=map.get("TGF").toString()
       XSSFRow row = sheet.createRow(i);

       if(i==0){
         XSSFCell glrcell = row.createCell(0);
         glrcell.setCellStyle(cellStyle);
         glrcell.setCellValue("管理人");

         XSSFCell cpmccell = row.createCell(1);
         cpmccell.setCellValue("产品名称"); 
         cpmccell.setCellStyle(cellStyle);

         XSSFCell glfzhmcell = row.createCell(2);
        glfzhmcell.setCellValue("管理费账户名");
        glfzhmcell.setCellStyle(cellStyle);

         XSSFCell glfzhcell = row.createCell(3);
         glfzhcell.setCellValue("管理费账号");
         glfzhcell.setCellStyle(cellStyle);

         XSSFCell glfkhhcell = row.createCell(4);
         glfkhhcell.setCellValue("管理费开户行");
         glfkhhcell.setCellStyle(cellStyle);

         XSSFCell glfdezfhcell = row.createCell(5);
         glfdezfhcell.setCellValue("管理费大额支付号");
         glfdezfhcell.setCellStyle(cellStyle);

         XSSFCell xzfwfzhmcell = row.createCell(6);
         xzfwfzhmcell.setCellValue("行政服务费账户名");
         xzfwfzhmcell.setCellStyle(cellStyle);

         XSSFCell xzfwzhcell = row.createCell(7);
         xzfwzhcell.setCellValue("行政服务费账号");
         xzfwzhcell.setCellStyle(cellStyle);

         XSSFCell xzfwkhhcell = row.createCell(8);
         xzfwkhhcell.setCellValue("行政服务费开户行");
         xzfwkhhcell.setCellStyle(cellStyle);

         XSSFCell xzfwdezfhcell = row.createCell(9);
         xzfwdezfhcell.setCellValue("行政服务费大额支付号");
         xzfwdezfhcell.setCellStyle(cellStyle);

         XSSFCell tgfzhmcell = row.createCell(10);
         tgfzhmcell.setCellValue("托管费账户名"); 
         tgfzhmcell.setCellStyle(cellStyle);

         XSSFCell tgfzhcell = row.createCell(11);
         tgfzhcell.setCellValue("托管费账号");
         tgfzhcell.setCellStyle(cellStyle);

         XSSFCell tgfkhhcell = row.createCell(12);
         tgfkhhcell.setCellValue("托管费开户行");
         tgfkhhcell.setCellStyle(cellStyle);

         XSSFCell tgfdezfhcell = row.createCell(13);
         tgfdezfhcell.setCellValue("托管费大额支付号");
         tgfdezfhcell.setCellStyle(cellStyle);

         XSSFCell tgfcell = row.createCell(14);
         tgfcell.setCellValue("托管费");
         tgfcell.setCellStyle(cellStyle);

         XSSFCell glfcell = row.createCell(15);
         glfcell.setCellValue("管理费"); 
         glfcell.setCellStyle(cellStyle);

         XSSFCell xzfwfcell = row.createCell(16);
         xzfwfcell.setCellValue("行政服务费");
         xzfwfcell.setCellStyle(cellStyle);

         XSSFCell zfycell = row.createCell(17);
         zfycell.setCellValue("总费用");
         zfycell.setCellStyle(cellStyle);


       }else{
         Map map = list.get(i-1);
         XSSFCell glrcell = row.createCell(0);
         glrcell.setCellValue(map.get("GLR").toString());
         glrcell.setCellStyle(cellStyle);            

         XSSFCell cpmccell = row.createCell(1);
         cpmccell.setCellValue(map.get("CPMC").toString());
         cpmccell.setCellStyle(cellStyle);           

         XSSFCell glfzhmcell = row.createCell(2);
         glfzhmcell.setCellValue(map.get("GLFZHM").toString());
         glfzhmcell.setCellStyle(cellStyle);

         XSSFCell glfzhcell = row.createCell(3);
         glfzhcell.setCellValue(map.get("GLFZHH").toString());
         glfzhcell.setCellStyle(cellStyle);

         XSSFCell glfkhhcell = row.createCell(4);
         glfkhhcell.setCellValue(map.get("GLFKHH").toString()); 
         glfkhhcell.setCellStyle(cellStyle);

         XSSFCell glfdezfhcell = row.createCell(5);
         glfdezfhcell.setCellValue(map.get("GLFDEZFH").toString());
         glfdezfhcell.setCellStyle(cellStyle);

         XSSFCell xzfwfzhmcell = row.createCell(6);
         xzfwfzhmcell.setCellValue(map.get("XZFWFZHM").toString());
         xzfwfzhmcell.setCellStyle(cellStyle);

         XSSFCell xzfwzhcell = row.createCell(7);
         xzfwzhcell.setCellValue(map.get("XZFWFZHH").toString());
         xzfwzhcell.setCellStyle(cellStyle);

         XSSFCell xzfwkhhcell = row.createCell(8);
         xzfwkhhcell.setCellValue(map.get("XZFWFKHH").toString());
         xzfwkhhcell.setCellStyle(cellStyle);

         XSSFCell xzfwdezfhcell = row.createCell(9);
         xzfwdezfhcell.setCellValue(map.get("XZFWFDEZFH").toString());
         xzfwdezfhcell.setCellStyle(cellStyle);

         XSSFCell tgfzhmcell = row.createCell(10);
         tgfzhmcell.setCellValue(map.get("TGFZHM").toString());
         tgfzhmcell.setCellStyle(cellStyle);

         XSSFCell tgfzhcell = row.createCell(11);
         tgfzhcell.setCellValue(map.get("TGFZHH").toString());
         tgfzhcell.setCellStyle(cellStyle);

         XSSFCell tgfkhhcell = row.createCell(12);
         tgfkhhcell.setCellValue(map.get("TGFKHH").toString()); 
         tgfkhhcell.setCellStyle(cellStyle);

         XSSFCell tgfdezfhcell = row.createCell(13);
         tgfdezfhcell.setCellValue(map.get("TGFDEZFH").toString());
         tgfdezfhcell.setCellStyle(cellStyle);

         XSSFCell tgfcell = row.createCell(14);
         tgfcell.setCellValue(map.get("TGF").toString());
         tgfcell.setCellStyle(cellStyle);

         XSSFCell glfcell = row.createCell(15);
         glfcell.setCellValue(map.get("GLF").toString()); 
         glfcell.setCellStyle(cellStyle);

         XSSFCell xzfwfcell = row.createCell(16);
         xzfwfcell.setCellValue(map.get("XZFWF").toString());
         xzfwfcell.setCellStyle(cellStyle);

         XSSFCell zfycell = row.createCell(17);
         zfycell.setCellValue(map.get("ZFY").toString()); 
         zfycell.setCellStyle(cellStyle);

   }
  // sheet.autoSizeColumn(14,1000000000000);
  // sheet.autoSizeColumn(15,1000000000000);
  //sheet.autoSizeColumn(16,1000000000000);
   //sheet.autoSizeColumn(17,1000000000000);
   sheet.setColumnWidth(14,22);

    这是代码,求大神帮助,无奈了

1个回答

weixin_39910677
weixin_39910677   2017.09.13 21:46

凡是做了什么跨咧居中啊,合并单元格的想自适应都没用,除非一个cell一个cell的给style

Csdn user default icon
上传中...
上传图片
插入图片