项目使用的是hhi框架,通过struts访问到action
xml文件:
<action name="fabAttributedaochu" class="com.FabAttributeAction" method="daochu" > <result name="success" type="stream"> <!-- 指定下载文件的文件类型 --> <param name="contentType">xls</param> <!-- 指定下载文件的文件位置 --> <param name="inputName">downExpFile</param> <!-- 指定下载文件的文件名 --> <param name="contentDisposition">attachment;filename="${contentDisposition}"</param> <!-- 指定下载文件的缓冲大小 --> <param name="bufferSize">4096</param> </result> </action>
action文件:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; private String contentDisposition;//一定要有的属性 public String getContentDisposition() { //get return contentDisposition;} public void setContentDisposition(String contentDisposition) { //set this.contentDisposition = contentDisposition; } public String daochu() throws Exception{//前台按导出键跳转到这个方法,可以在这里设一些判断条件是否继续 return success; } public InputStream getDownExpFile() throws Exception{//根据xml的返回定义了一个get值,进入之个方法, contentDisposition = java.net.URLEncoder.encode("buyOrder.xls","UTF-8");//指定一个文件的文件名 return fabAttributeBo.daochu();//调用bo层数据返回一个InputStream对象 }
逻辑业务层(bo):
/** * 导出 * @throws BOException * @throws FileNotFoundException * */ public FileInputStream daochu() throws BOException { try { List<FabAttributeVo> list =this.getFabAttributeDao().selectFabAttribute(null).getList();//导出数据的集合--数据 Integer rowNumber =list.size(); //创建新的ExCEL HSSFWorkbook workbook = new HSSFWorkbook(); //创建工作表名 HSSFSheet sheet = workbook.createSheet("标签1"); //定义行 HSSFRow row =null; //TITlelist为单元格明 List<String > titleList = this.obtainTitleList();//调用已定义好的列抬头 //用数组定义单元格 HSSFCell [] hssfCells = new HSSFCell[titleList.size()]; //设置单元格格式 HSSFCellStyle cellStyle = workbook.createCellStyle(); HSSFDataFormat format = workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("@")); //下面是创建首行 row =sheet.createRow(0); for(int j=0;j<titleList.size();j++){ hssfCells[j] = row.createCell(j); hssfCells[j].setCellStyle(cellStyle); hssfCells[j].setCellValue(titleList.get(j)); } //下面是创建数据行 for (int i = 1; rowNumber-i>=0; i++) { row = sheet.createRow(i); for (int j = 0; j < titleList.size(); j++) { hssfCells[j]=row.createCell(j); hssfCells[j].setCellStyle(cellStyle); hssfCells[j].setCellType(HSSFCell.CELL_TYPE_STRING); switch (j) {//每一个行需要插入拿一条 case 0:hssfCells[j].setCellValue(list.get(i-1).getAttr_id());break; case 1:hssfCells[j].setCellValue(list.get(i-1).getType_name());break; case 2:hssfCells[j].setCellValue(list.get(i-1).getAttr_name());break; case 3:hssfCells[j].setCellValue(list.get(i-1).getAttr_input_type());break; case 4:hssfCells[j].setCellValue(list.get(i-1).getAttr_values());break; case 5:hssfCells[j].setCellValue(list.get(i-1).getSortOrder());break; } } } File file =new File(this.getClass().getClassLoader().getResource("").getPath() + "fabric/tmp/buyOrder.xls"); if(file.exists())file.delete(); //新建一个输出文件流 FileOutputStream fout = new FileOutputStream(file); //写相应的Excel工作薄 workbook.write(fout); FileInputStream in= new FileInputStream(file); fout.close(); return in; }catch(Exception e){ e.printStackTrace(); } return null; }
设定表格的首列行:
/** * 用于列名的设置 */ private List<String> obtainTitleList(){//自己定义好,你需要的eml表格中的列 List<String> titleList = new ArrayList<String>(); titleList.add("ID"); titleList.add("商品类型名称"); titleList.add("属性名称"); titleList.add("输入类型"); titleList.add("属性值"); titleList.add("排列次序"); return titleList; }
这样就ok了!!
时间: 2024-10-13 15:09:34