struts2+poi 对excel进行导出

本文介绍如何使用Struts2框架实现从网页表单数据到Excel文件的导出功能。通过具体代码示例展示了如何配置Struts2 Action来处理导出请求,并利用HSSFWorkbook进行Excel文件的创建及格式化。

页面的话传一个集合给action

页面list name取值 格式为list[i].属性名 由于用到struts2标签 i的值等于

<s:iterator value="carlist" status="num">
 <tr>
     <td><input type="text" name="carlist[<s:property value='#num.index'/>].//这里不用拼接直接用infolicense" value="${infolicense}" /><s:property value="infolicense"/> </td>
              <td><input type="text" name="carlist[<s:property value='#num.index'/>].tcmwholename" value="${ tcmwholename}" /><s:property value="tcmwholename" /> </td>
              <td><input type="text" name="carlist[<s:property value='#num.index'/>].attiname" value="${ attiname}" /><s:property value="attiname"/></td>
              <td><input type="text" name="carlist[<s:property value='#num.index'/>].deptname" value="${ deptname}" /><s:property value="deptname"/></td>
</tr>




private String fileNames;//提供get/set方法
private InputStream excelStream;//

public String doExport() throws TargetException{

        String result = "";
        String tmpContent="编号,车型,使用公司,使用部门";//标题
        String tmpContentCn="编号,车型,使用公司,使用部门";
                HSSFWorkbook workbook = printExcel(tmpContent,tmpContentCn,carlist);
          
               if(workbook != null){
                      try{
                              Calendar c = Calendar.getInstance();
                             int year = c.get(Calendar.YEAR);
                              int month = c.get(Calendar.MONTH)+1;
                              String month_ = new String(""+month);
                              if(month<10){
                                    month_ = "0"+month;
                              }
                              int day = c.get(Calendar.DAY_OF_MONTH);
                              String day_ = new String(""+day);
                             if(day<10){
                                    day_ = "0"+day;
                               }
                               this.exportExcel(workbook,year+month_+""+day_+"sxta.xls");
                              result = "outExcel";
                        }catch(IOException e){
                              e.printStackTrace();
                              //message = "出错了!";
                             // redirectTo = this.getHttpServletRequest().getContextPath()+"/task/listProject.action";
                              //return SUCCESS;
                        }
               }
      
       return result;
}
private HSSFWorkbook printExcel(String tmpContent,String tmpContentCn,List dataList){
System.out.println("+++++++++++++++++++++"+carlist.size());
         HSSFWorkbook workbook = null;
         String[] titles_CN = tmpContentCn.split(",");
         String[] titles_EN = tmpContent.split(",");
         try{
             //创建工作簿实例
               workbook = new HSSFWorkbook();
             //创建工作表实例
             HSSFSheet sheet = workbook.createSheet("sxtaExcel");
             sheet.setDefaultColumnWidth((short) 25);
             //设置列宽
              this.setSheetColumnWidth(titles_CN,sheet);
          // 获取样式
              HSSFCellStyle style = this.createTitleStyle(workbook);
              HSSFCellStyle style1 = this.createTitleStyle1(workbook);
              if(dataList != null){
                  //创建第一行标题
                    HSSFRow row = sheet.createRow((short)0);// 建立新行
                    for(int i=0;i<titles_CN.length;i++){
                    this.createCell(row, i, style, HSSFCell.CELL_TYPE_STRING,
                           this.getText(titles_CN[i]));
                    }
                   //给excel填充数据
                   for(int i=0;i<dataList.size();i++){
                            //将dataList里面的数据取出来
                           AppCarinfo c= (AppCarinfo)dataList.get(i);
                             HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行
                  
                            boolean isOverTime = false;
                             for(int j=0;j<titles_EN.length;j++){
                                      String tmpstr = "";
                                      if (titles_EN[j].equals("编号")){
                                     System.out.println("======================");
                                            this.createCell(row1, j, style1, HSSFCell.CELL_TYPE_STRING,c.getInfolicense());
                                      }else if(titles_EN[j].equals("车型")){
                                     this.createCell(row1, j, style1, HSSFCell.CELL_TYPE_STRING,c.getTcmwholename());
                                      }else if(titles_EN[j].equals("使用公司")){
                                     this.createCell(row1, j, style1, HSSFCell.CELL_TYPE_STRING,c.getAttiname());
                                      }else if(titles_EN[j].equals("使用部门")){
                                     this.createCell(row1, j, style1, HSSFCell.CELL_TYPE_STRING,c.getDeptname());
                                      }
                          }
                 
                  }
           }else{
                    this.createCell(sheet.createRow(0), 0, style,HSSFCell.CELL_TYPE_STRING, "查无资料");
           }
      }catch(Exception e){
                  e.printStackTrace();
      }
     return workbook;
}
//设置列宽
private void setSheetColumnWidth(String[] titles_CN,HSSFSheet sheet){
       //根据你数据里面的记录有多少列,就设置多少列
      for(int i=0;i<titles_CN.length;i++){
              sheet.setColumnWidth((short)i, (short) 3000);
      }
}


//设置excel的title样式  
private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
HSSFCellStyle style = wb.createCellStyle();
    // 设置这些样式
    style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    // 生成一个字体
    HSSFFont font = wb.createFont();
    font.setColor(HSSFColor.VIOLET.index);
    font.setFontHeightInPoints((short) 12);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    // 把字体应用到当前的样式
    style.setFont(font);
      return style;  
}
private HSSFCellStyle createTitleStyle1(HSSFWorkbook wb) {
HSSFCellStyle style2 = wb.createCellStyle();
    style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
    style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    // 生成另一个字体
    HSSFFont font2 = wb.createFont();
    font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
    // 把字体应用到当前的样式
    style2.setFont(font2);
      return style2;  
}
   
//创建Excel单元格  
private void createCell(HSSFRow row, int column, HSSFCellStyle style,int cellType,Object value) {
      HSSFCell cell = row.createCell((short) column);
     //cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//      style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//      style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//      style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//      style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//      style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//      style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//      style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
      // 把字体应用到当前的样式
      if (style != null) {
           cell.setCellStyle(style);
      }  
      switch(cellType){
           case HSSFCell.CELL_TYPE_BLANK: {} break;
           case HSSFCell.CELL_TYPE_STRING: {
          System.out.println("进来了没");
          cell.setCellValue(value.toString()+"");
          } 
           break;
           case HSSFCell.CELL_TYPE_NUMERIC: {
           cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
               cell.setCellValue(Double.parseDouble(value.toString()));}break;
           default: break;
     }  
}
//写入输入流中
private void exportExcel(HSSFWorkbook workbook,String fileName) throws IOException{
       fileNames = fileName;
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       workbook.write(baos);
       baos.flush();
       byte[] aa = baos.toByteArray();
       excelStream = new ByteArrayInputStream(aa, 0, aa.length);
       baos.close();

}


配置文件

<action name="doExport" class="actions.car.annualcheck.CarCheckAction" method="doExport">
<result name="outExcel" type="stream">
                     <param name="contentType">application/vnd.ms-excel</param>   <!-- 注意这里的ContentType -->
                     <param name="inputName">excelStream</param>
                     <param name="contentDisposition">attachment;filename="${fileNames}"</param><!-- filename跟action的一致-->
                     <param name="bufferSize">1024</param>
</result>
</action>

内容概要:本文介绍了一套针对智能穿戴设备的跑步/骑行轨迹记录系统实战方案,旨在解决传统运动APP存在的定位漂移、数据断层和路径分析单一等问题。系统基于北斗+GPS双模定位、惯性测量单元(IMU)和海拔传感器,实现高精度轨迹采集,并通过卡尔曼滤波算法修正定位误差,在信号弱环境下利用惯性导航补位,确保轨迹连续性。系统支持跑步与骑行两种场景的差异化功能,包括实时轨迹记录、多维度路径分析(如配速、坡度、能耗)、数据可视化(地图标注、曲线图、3D回放)、异常提醒及智能优化建议,并可通过蓝牙/Wi-Fi同步数据至手机APP,支持社交分享与专业软件导出。技术架构涵盖硬件层、设备端与手机端软件层以及云端数据存储,强调低功耗设计与用户体验优化。经过实测验证,系统在定位精度、续航能力和场景识别准确率方面均达到预期指标,具备良好的实用性和扩展性。; 适合人群:具备一定嵌入式开发或移动应用开发经验,熟悉物联网、传感器融合与数据可视化的技术人员,尤其是从事智能穿戴设备、运动健康类产品研发的工程师和产品经理;也适合高校相关专业学生作为项目实践参考。; 使用场景及目标:① 开发高精度运动轨迹记录功能,解决GPS漂移与断点问题;② 实现跑步与骑行场景下的差异化数据分析与个性化反馈;③ 构建完整的“终端采集-手机展示-云端存储”系统闭环,支持社交互动与商业拓展;④ 掌握低功耗优化、多源数据融合、动态功耗调节等关键技术在穿戴设备中的落地应用。; 阅读建议:此资源以真实项目为导向,不仅提供详细的技术实现路径,还包含硬件选型、测试验证与商业扩展思路,建议读者结合自身开发环境,逐步实现各模块功能,重点关注定位优化算法、功耗控制策略与跨平台数据同步机制的设计与调优。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值