Struts 2导出EXCEL

  1. import java.io.IOException;   
  2. import java.io.OutputStream;   
  3. import java.util.List;   
  4. import javax.servlet.http.HttpServletResponse;   
  5. import org.apache.poi.hssf.usermodel.HSSFCell;   
  6. import org.apache.poi.hssf.usermodel.HSSFCellStyle;   
  7. import org.apache.poi.hssf.usermodel.HSSFDataFormat;   
  8. import org.apache.poi.hssf.usermodel.HSSFFont;   
  9. import org.apache.poi.hssf.usermodel.HSSFRow;   
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  12. import org.apache.struts2.ServletActionContext;   
  13. import com.neusoft.dcd.css.common.BaseAction;   
  14. import com.neusoft.dcd.css.model.management.user.UserBaseInfo;   
  15. import com.neusoft.dcd.css.model.tsc.Tsc;   
  16. import com.neusoft.dcd.css.service.tsc.TscService;   
  17. import com.opensymphony.xwork2.Preparable;   
  18.   
  19.   
  20. public class ExportExcel {     
  21.     private static final long serialVersionUID = 1L;   
  22.     private TscService cssTscService;   
  23.     public void prepare() throws Exception {   
  24.     }   
  25.   
  26.     /**  
  27.      * 绘出Excel  
  28.      *   
  29.      * @param actionMapping  
  30.      * @param actionForm  
  31.      * @param request  
  32.      * @param response  
  33.      * @return  
  34.      * @throws Exception   
  35.      */  
  36.     public String init() throws Exception {   
  37.         UserBaseInfo userinfo = (UserBaseInfo) ServletActionContext   
  38.         .getRequest().getSession().getAttribute("UserBaseInfo");   
  39.         if(userinfo!=null){   
  40.         HttpServletResponse response = ServletActionContext.getResponse();    
  41.         List dataList=cssTscService.queryForProTsclb(userinfo.getUserId());    
  42.         HSSFWorkbook workbook = exportExcel(dataList);   
  43.         if(workbook!= null){   
  44.             this.printExcel(workbook,response,"ExcelTsc.xls");   
  45.         }   
  46.         }   
  47.         return SUCCESS;   
  48.     }   
  49.   
  50.   
  51. // 导出Excel   
  52.   
  53.         private void printExcel(HSSFWorkbook workbook,HttpServletResponse response, String string) throws IOException {   
  54.         OutputStream out = response.getOutputStream();   
  55.         response.setHeader("Content-disposition","attachment; filename=" + "TSC.xls");   
  56.         response.setContentType("application/msexcel;charset=UTF-8");   
  57.         workbook.write(out);   
  58.         out.flush();   
  59.         out.close();   
  60.            
  61.     }   
  62.   
  63.     public HSSFWorkbook exportExcel(List dataList) throws Exception   
  64.     {   
  65.          HSSFWorkbook workbook = null;   
  66.          try  
  67.          {   
  68.               // 这里的数据即时你要从后台取得的数据   
  69.                  
  70.               // 创建工作簿实例   
  71.               workbook = new HSSFWorkbook();   
  72.               // 创建工作表实例   
  73.               HSSFSheet sheet = workbook.createSheet("TscExcel");   
  74.               // 设置列宽   
  75.               this.setSheetColumnWidth(sheet);   
  76.               // 获取样式   
  77.               HSSFCellStyle style = this.createTitleStyle(workbook);   
  78.                  
  79.              //   
  80.              if (dataList != null && dataList.size() > 0)   
  81.              {   
  82.                   // 创建第一行标题,标题名字的本地信息通过resources从资源文件中获取   
  83.                   HSSFRow row = sheet.createRow((short0);// 建立新行   
  84.                     
  85.                   this.createCell(row, 0, style, HSSFCell.CELL_TYPE_STRING,   
  86.                        this.getText("css.tsc.asktime"));   
  87.                   this.createCell(row, 1, style, HSSFCell.CELL_TYPE_STRING,   
  88.                           this.getText("css.tsc.productname"));   
  89.                   this.createCell(row, 2, style, HSSFCell.CELL_TYPE_STRING,   
  90.                           this.getText("css.tsc.passsort"));   
  91.                   this.createCell(row, 3, style, HSSFCell.CELL_TYPE_STRING,   
  92.                           this.getText("css.tsc.askusername"));   
  93.                   this.createCell(row, 4, style, HSSFCell.CELL_TYPE_STRING,   
  94.                           this.getText("css.tsc.dtype"));   
  95.                   this.createCell(row, 5, style, HSSFCell.CELL_TYPE_STRING,   
  96.                           this.getText("css.tsc.askone"));   
  97.                   this.createCell(row, 6, style, HSSFCell.CELL_TYPE_STRING,   
  98.                           this.getText("css.tsc.department"));   
  99.                   this.createCell(row, 7, style, HSSFCell.CELL_TYPE_STRING,   
  100.                           this.getText("css.tsc.requesttime"));   
  101.                   this.createCell(row, 8, style, HSSFCell.CELL_TYPE_STRING,   
  102.                           this.getText("css.tsc.answertime"));   
  103.                   this.createCell(row, 9, style, HSSFCell.CELL_TYPE_STRING,   
  104.                           this.getText("css.tsc.requesttow"));   
  105.                   // 给excel填充数据   
  106.                   for(int i=0;i<dataList.size();i++)   
  107.                   {   
  108.                       // 将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦   
  109.                       Tsc  model= (Tsc)dataList.get(i);   
  110.                       HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行   
  111.                       if(model.getAsktime()!=null)   
  112.                       this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING,   
  113.                            model.getAsktime());   
  114.                       if(model.getProductname()!=null)   
  115.                       this.createCell(row1, 1, style, HSSFCell.CELL_TYPE_STRING,   
  116.                            model.getProductname());     
  117.                       if(model.getPasssort()!=null)   
  118.                       this.createCell(row1, 2, style, HSSFCell.CELL_TYPE_STRING,   
  119.                            model.getPasssort());   
  120.                       if(model.getAskusername()!=null)   
  121.                       this.createCell(row1, 3, style, HSSFCell.CELL_TYPE_STRING,   
  122.                            model.getAskusername());   
  123.                       if(model.getDtype()!=null)   
  124.                       this.createCell(row1, 4, style, HSSFCell.CELL_TYPE_STRING,   
  125.                             model.getDtype());   
  126.                       if(model.getAskone()!=null)   
  127.                       this.createCell(row1, 5, style, HSSFCell.CELL_TYPE_STRING,   
  128.                             model.getAskone());   
  129.                       if(model.getDepartment()!=null)   
  130.                       this.createCell(row1, 6, style, HSSFCell.CELL_TYPE_STRING,   
  131.                             model.getDepartment());   
  132.                       if(model.getRequesttime()!=null)   
  133.                       this.createCell(row1, 7, style, HSSFCell.CELL_TYPE_STRING,   
  134.                             model.getRequesttime());   
  135.                       if(model.getAnswertime()!=null)   
  136.                       this.createCell(row1, 8, style, HSSFCell.CELL_TYPE_STRING,   
  137.                             model.getAnswertime());   
  138.                       if(model.getRequesttow()!=null)   
  139.                       this.createCell(row1, 9, style, HSSFCell.CELL_TYPE_STRING,   
  140.                             model.getRequesttow());   
  141.                      
  142.                   }   
  143.              }   
  144.              else {   
  145.                  this.createCell(sheet.createRow(0), 0, style,   
  146.                  HSSFCell.CELL_TYPE_STRING, "查无资料");   
  147.              }   
  148.          }catch(Exception e)   
  149.          {   
  150.              e.printStackTrace();   
  151.          }   
  152.         return workbook;   
  153.   
  154.     }   
  155.     private void setSheetColumnWidth(HSSFSheet sheet)   
  156.     {   
  157.             // 根据你数据里面的记录有多少列,就设置多少列   
  158.             sheet.setColumnWidth((short0, (short3000);   
  159.             sheet.setColumnWidth((short1, (short3000);   
  160.             sheet.setColumnWidth((short2, (short3000);   
  161.             sheet.setColumnWidth((short3, (short3000);   
  162.             sheet.setColumnWidth((short4, (short5000);   
  163.             sheet.setColumnWidth((short5, (short5000);   
  164.             sheet.setColumnWidth((short6, (short5000);   
  165.             sheet.setColumnWidth((short7, (short5000);   
  166.             sheet.setColumnWidth((short8, (short5000);   
  167.             sheet.setColumnWidth((short9, (short5000);   
  168.     }   
  169.     // 设置excel的title样式   
  170.     private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {   
  171.        HSSFFont boldFont = wb.createFont();   
  172.        boldFont.setFontHeight((short200);   
  173.        HSSFCellStyle style = wb.createCellStyle();   
  174.        style.setFont(boldFont);   
  175.        style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));   
  176.        return style;   
  177.    }   
  178.    // 创建Excel单元格   
  179.    private void createCell(HSSFRow row, int column, HSSFCellStyle style,   
  180.            int cellType,Object value) {   
  181.            HSSFCell cell = row.createCell((short) column);   
  182.            cell.setEncoding(HSSFCell.ENCODING_UTF_16);   
  183.            if (style != null) {   
  184.               cell.setCellStyle(style);   
  185.           }    
  186.           switch (cellType) {   
  187.               case HSSFCell.CELL_TYPE_BLANK: {   
  188.        }   
  189.            break;   
  190.        case HSSFCell.CELL_TYPE_STRING: {   
  191.            cell.setCellValue(value.toString());   
  192.             }   
  193.            break;   
  194.        case HSSFCell.CELL_TYPE_NUMERIC: {   
  195.            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);   
  196.            // DecimalFormat format = new DecimalFormat("###,##0.00");   
  197.            // cell.setCellValue(Float.parseFloat(value.toString()));   
  198.            cell.setCellValue(Double.parseDouble(value.toString()));   
  199.        }   
  200.            break;   
  201.        default:   
  202.            break;   
  203.        }   
  204.    }   
  205.   
  206. public void setCssTscService(TscService cssTscService) {   
  207.     this.cssTscService = cssTscService;   
  208. }   
  209.   
  210. }  

 

  1. import java.io.IOException;   
  2. import java.io.OutputStream;   
  3. import java.util.List;   
  4. import javax.servlet.http.HttpServletResponse;   
  5. import org.apache.poi.hssf.usermodel.HSSFCell;   
  6. import org.apache.poi.hssf.usermodel.HSSFCellStyle;   
  7. import org.apache.poi.hssf.usermodel.HSSFDataFormat;   
  8. import org.apache.poi.hssf.usermodel.HSSFFont;   
  9. import org.apache.poi.hssf.usermodel.HSSFRow;   
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  12. import org.apache.struts2.ServletActionContext;   
  13. import com.neusoft.dcd.css.common.BaseAction;   
  14. import com.neusoft.dcd.css.model.management.user.UserBaseInfo;   
  15. import com.neusoft.dcd.css.model.tsc.Tsc;   
  16. import com.neusoft.dcd.css.service.tsc.TscService;   
  17. import com.opensymphony.xwork2.Preparable;   
  18.   
  19.   
  20. public class ExportExcel {     
  21.     private static final long serialVersionUID = 1L;   
  22.     private TscService cssTscService;   
  23.     public void prepare() throws Exception {   
  24.     }   
  25.   
  26.     /**  
  27.      * 绘出Excel  
  28.      *   
  29.      * @param actionMapping  
  30.      * @param actionForm  
  31.      * @param request  
  32.      * @param response  
  33.      * @return  
  34.      * @throws Exception   
  35.      */  
  36.     public String init() throws Exception {   
  37.         UserBaseInfo userinfo = (UserBaseInfo) ServletActionContext   
  38.         .getRequest().getSession().getAttribute("UserBaseInfo");   
  39.         if(userinfo!=null){   
  40.         HttpServletResponse response = ServletActionContext.getResponse();    
  41.         List dataList=cssTscService.queryForProTsclb(userinfo.getUserId());    
  42.         HSSFWorkbook workbook = exportExcel(dataList);   
  43.         if(workbook!= null){   
  44.             this.printExcel(workbook,response,"ExcelTsc.xls");   
  45.         }   
  46.         }   
  47.         return SUCCESS;   
  48.     }   
  49.   
  50.   
  51. // 导出Excel   
  52.   
  53.         private void printExcel(HSSFWorkbook workbook,HttpServletResponse response, String string) throws IOException {   
  54.         OutputStream out = response.getOutputStream();   
  55.         response.setHeader("Content-disposition","attachment; filename=" + "TSC.xls");   
  56.         response.setContentType("application/msexcel;charset=UTF-8");   
  57.         workbook.write(out);   
  58.         out.flush();   
  59.         out.close();   
  60.            
  61.     }   
  62.   
  63.     public HSSFWorkbook exportExcel(List dataList) throws Exception   
  64.     {   
  65.          HSSFWorkbook workbook = null;   
  66.          try  
  67.          {   
  68.               // 这里的数据即时你要从后台取得的数据   
  69.                  
  70.               // 创建工作簿实例   
  71.               workbook = new HSSFWorkbook();   
  72.               // 创建工作表实例   
  73.               HSSFSheet sheet = workbook.createSheet("TscExcel");   
  74.               // 设置列宽   
  75.               this.setSheetColumnWidth(sheet);   
  76.               // 获取样式   
  77.               HSSFCellStyle style = this.createTitleStyle(workbook);   
  78.                  
  79.              //   
  80.              if (dataList != null && dataList.size() > 0)   
  81.              {   
  82.                   // 创建第一行标题,标题名字的本地信息通过resources从资源文件中获取   
  83.                   HSSFRow row = sheet.createRow((short0);// 建立新行   
  84.                     
  85.                   this.createCell(row, 0, style, HSSFCell.CELL_TYPE_STRING,   
  86.                        this.getText("css.tsc.asktime"));   
  87.                   this.createCell(row, 1, style, HSSFCell.CELL_TYPE_STRING,   
  88.                           this.getText("css.tsc.productname"));   
  89.                   this.createCell(row, 2, style, HSSFCell.CELL_TYPE_STRING,   
  90.                           this.getText("css.tsc.passsort"));   
  91.                   this.createCell(row, 3, style, HSSFCell.CELL_TYPE_STRING,   
  92.                           this.getText("css.tsc.askusername"));   
  93.                   this.createCell(row, 4, style, HSSFCell.CELL_TYPE_STRING,   
  94.                           this.getText("css.tsc.dtype"));   
  95.                   this.createCell(row, 5, style, HSSFCell.CELL_TYPE_STRING,   
  96.                           this.getText("css.tsc.askone"));   
  97.                   this.createCell(row, 6, style, HSSFCell.CELL_TYPE_STRING,   
  98.                           this.getText("css.tsc.department"));   
  99.                   this.createCell(row, 7, style, HSSFCell.CELL_TYPE_STRING,   
  100.                           this.getText("css.tsc.requesttime"));   
  101.                   this.createCell(row, 8, style, HSSFCell.CELL_TYPE_STRING,   
  102.                           this.getText("css.tsc.answertime"));   
  103.                   this.createCell(row, 9, style, HSSFCell.CELL_TYPE_STRING,   
  104.                           this.getText("css.tsc.requesttow"));   
  105.                   // 给excel填充数据   
  106.                   for(int i=0;i<dataList.size();i++)   
  107.                   {   
  108.                       // 将dataList里面的数据取出来,假设这里取出来的是Model,也就是某个javaBean的意思啦   
  109.                       Tsc  model= (Tsc)dataList.get(i);   
  110.                       HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行   
  111.                       if(model.getAsktime()!=null)   
  112.                       this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING,   
  113.                            model.getAsktime());   
  114.                       if(model.getProductname()!=null)   
  115.                       this.createCell(row1, 1, style, HSSFCell.CELL_TYPE_STRING,   
  116.                            model.getProductname());     
  117.                       if(model.getPasssort()!=null)   
  118.                       this.createCell(row1, 2, style, HSSFCell.CELL_TYPE_STRING,   
  119.                            model.getPasssort());   
  120.                       if(model.getAskusername()!=null)   
  121.                       this.createCell(row1, 3, style, HSSFCell.CELL_TYPE_STRING,   
  122.                            model.getAskusername());   
  123.                       if(model.getDtype()!=null)   
  124.                       this.createCell(row1, 4, style, HSSFCell.CELL_TYPE_STRING,   
  125.                             model.getDtype());   
  126.                       if(model.getAskone()!=null)   
  127.                       this.createCell(row1, 5, style, HSSFCell.CELL_TYPE_STRING,   
  128.                             model.getAskone());   
  129.                       if(model.getDepartment()!=null)   
  130.                       this.createCell(row1, 6, style, HSSFCell.CELL_TYPE_STRING,   
  131.                             model.getDepartment());   
  132.                       if(model.getRequesttime()!=null)   
  133.                       this.createCell(row1, 7, style, HSSFCell.CELL_TYPE_STRING,   
  134.                             model.getRequesttime());   
  135.                       if(model.getAnswertime()!=null)   
  136.                       this.createCell(row1, 8, style, HSSFCell.CELL_TYPE_STRING,   
  137.                             model.getAnswertime());   
  138.                       if(model.getRequesttow()!=null)   
  139.                       this.createCell(row1, 9, style, HSSFCell.CELL_TYPE_STRING,   
  140.                             model.getRequesttow());   
  141.                      
  142.                   }   
  143.              }   
  144.              else {   
  145.                  this.createCell(sheet.createRow(0), 0, style,   
  146.                  HSSFCell.CELL_TYPE_STRING, "查无资料");   
  147.              }   
  148.          }catch(Exception e)   
  149.          {   
  150.              e.printStackTrace();   
  151.          }   
  152.         return workbook;   
  153.   
  154.     }   
  155.     private void setSheetColumnWidth(HSSFSheet sheet)   
  156.     {   
  157.             // 根据你数据里面的记录有多少列,就设置多少列   
  158.             sheet.setColumnWidth((short0, (short3000);   
  159.             sheet.setColumnWidth((short1, (short3000);   
  160.             sheet.setColumnWidth((short2, (short3000);   
  161.             sheet.setColumnWidth((short3, (short3000);   
  162.             sheet.setColumnWidth((short4, (short5000);   
  163.             sheet.setColumnWidth((short5, (short5000);   
  164.             sheet.setColumnWidth((short6, (short5000);   
  165.             sheet.setColumnWidth((short7, (short5000);   
  166.             sheet.setColumnWidth((short8, (short5000);   
  167.             sheet.setColumnWidth((short9, (short5000);   
  168.     }   
  169.     // 设置excel的title样式   
  170.     private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {   
  171.        HSSFFont boldFont = wb.createFont();   
  172.        boldFont.setFontHeight((short200);   
  173.        HSSFCellStyle style = wb.createCellStyle();   
  174.        style.setFont(boldFont);   
  175.        style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));   
  176.        return style;   
  177.    }   
  178.    // 创建Excel单元格   
  179.    private void createCell(HSSFRow row, int column, HSSFCellStyle style,   
  180.            int cellType,Object value) {   
  181.            HSSFCell cell = row.createCell((short) column);   
  182.            cell.setEncoding(HSSFCell.ENCODING_UTF_16);   
  183.            if (style != null) {   
  184.               cell.setCellStyle(style);   
  185.           }    
  186.           switch (cellType) {   
  187.               case HSSFCell.CELL_TYPE_BLANK: {   
  188.        }   
  189.            break;   
  190.        case HSSFCell.CELL_TYPE_STRING: {   
  191.            cell.setCellValue(value.toString());   
  192.             }   
  193.            break;   
  194.        case HSSFCell.CELL_TYPE_NUMERIC: {   
  195.            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);   
  196.            // DecimalFormat format = new DecimalFormat("###,##0.00");   
  197.            // cell.setCellValue(Float.parseFloat(value.toString()));   
  198.            cell.setCellValue(Double.parseDouble(value.toString()));   
  199.        }   
  200.            break;   
  201.        default:   
  202.            break;   
  203.        }   
  204.    }   
  205.   
  206. public void setCssTscService(TscService cssTscService) {   
  207.     this.cssTscService = cssTscService;   
  208. }   
  209.   
  210. }  
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值