`
zhangchunxiao
  • 浏览: 15558 次
社区版块
存档分类
最新评论

导出excel的两种方式(一)

    博客分类:
  • Java
 
阅读更多

1.导出excel方法调用:

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.OutputStream;

@Controller
@RequestMapping(value = "/collection")
public class CollectionAction {
 @RequestMapping("/exportExcel4ServiceFee")
    public void exportExcel4ServiceFee(LoanSplitModel loanSplitModel,HttpServletRequest request, HttpServletResponse response) {
		PageView<LoanSplitModel> pageView = collectionService.downPageView4RemindersServiceFee(loanSplitModel);
        int count = 1;
        List<String[]> list = new ArrayList<String[]>();
        list.add(new String[]{"序号","合同编号","客户姓名","合同金额"});
        for (LoanSplitModel item : pageView.getRecords()) {
            StringBuffer sb = new StringBuffer();
            sb = sb.append(count++).append(",")//序号
                   .append(item.getContractCode() != null ? item.getContractCode() : "").append(",")//合同编号
                   .append(item.getCustomerName() != null ? item.getCustomerName() : "").append(",")//客户姓名
                   .append(item.getContractMoney() != null ? df.format(item.getContractMoney()) : "").append(",");//合同金额    
            list.add(sb.toString().split(","));
        }
        HSSFWorkbook workbook = ExcelUtil.addHSSFSheet(list);
        try {
            OutputStream os = ExcelUtil.getRespone("服务费统计表.xls", response);
            workbook.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
        	e.printStackTrace();
            logger.error("-----------服务费统计列表导出excel异常", e);
        }
    }

 调用的ExcelUtil类如下:

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

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;

public class ExcelUtil {
	public static HSSFWorkbook addHSSFSheet(List<String[]> list) {
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 产生工作表对象
		HSSFSheet sheet = workbook.createSheet();
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
		HSSFCellStyle numCellStyle = workbook.createCellStyle();
		numCellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
		numCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
		HSSFRow row = sheet.createRow((int) 0);// 创建一行
		  
		HSSFCell cell = row.createCell((int) 0);// 创建一列

		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow((int) (i));// 创建一行
			String[] files = list.get(i);
			for (int k = 0; k < files.length; k++) {
				sheet.setColumnWidth( k, 5900);// 设置列宽  
				cell = row.createCell((int) k);// 创建一列
				cell.setCellStyle(cellStyle);
				Pattern pNum=Pattern.compile("^(([1-9]\\d{0,9})|0)(\\.\\d{2})+$"); 
				if(files[k] == null || !pNum.matcher(files[k]).matches()){
					cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					cell.setCellValue(files[k]!=null?files[k]:"");
				}else{
					
					cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
					cell.setCellStyle(numCellStyle);
					cell.setCellValue(Double.parseDouble(files[k]));
				}
			}
		}
		return workbook;
	}
	
	public static OutputStream getRespone(String filename,
			HttpServletResponse response) {
		OutputStream os = null;
		try {
			filename = new String(filename.getBytes("GBK"), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName="
				+ filename);
		try {
			os = response.getOutputStream();
		} catch (IOException e) {
		}
		return os;
	}
	  
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics