根据模板导出到Excel(lp)

// 获取模板
  String dir = servlet.getServletContext().getRealPath(
    "/template/test.xls");
  POIFSFileSystem fis = new POIFSFileSystem(new FileInputStream(dir));
  HSSFWorkbook wb = new HSSFWorkbook(fis);
  HSSFSheet sheet1 = wb.getSheetAt(0);

  // 设定单元格值
      // ................
  
   // out to excel
  try {
   response.setContentType("application/ms-excel; charset=/"utf-8/"");
   response.setHeader("Content-disposition", "attachment;filename="
     + "testOut.xls");
   OutputStream out = response.getOutputStream();

   wb.write(out);
   out.close();

 } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } 

附自用的excelutil

import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;


public class ExcelUtil {

//设定单元格的值
 public static void setSheetCellValue(HSSFSheet sheet, int rowIndex,
   short colIndex, Object value) {
  HSSFCell cell = ExcelUtil.ensureCellExist(sheet, rowIndex, colIndex);
  if (value instanceof Date) {
   // date type
   cell.setCellValue((Date) value);
  } else if (value instanceof Number) {
   // numeric type: Double,Float,Integer and so on.
   cell.setCellValue(((Number) value).doubleValue());
  } else if (value instanceof String) {
   // String type
   
   // String encoding
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   //
   String strValue = (String) value;
   strValue = strValue.replaceAll("/r/n", "/n");
   cell.setCellValue(strValue);
  }
 }

//设定单元格的规则 

public static void setSheetCellFormula(HSSFSheet sheet, int rowIndex,
   short colIndex, String formula) {
  HSSFCell cell = ExcelUtil.ensureCellExist(sheet, rowIndex, colIndex);
  cell.setCellFormula(formula);
 }

//设定单元格的格式

 public static void setSheetCellStyle(HSSFSheet sheet, int rowIndex,
   short colIndex, HSSFCellStyle style) {
  HSSFCell cell = ExcelUtil.ensureCellExist(sheet, rowIndex, colIndex);
  cell.setCellStyle(style);
 }

//获取单元格的格式

 public static HSSFCellStyle getSheetCellStyle(HSSFSheet sheet,
   int rowIndex, short colIndex) {
  HSSFCell cell = ExcelUtil.ensureCellExist(sheet, rowIndex, colIndex);
  return cell.getCellStyle();
 }

//获得一个单元格,如果不存在,新建 

public static HSSFCell ensureCellExist(HSSFSheet sheet, int rowIndex,
   short colIndex) {
  HSSFRow row = sheet.getRow(rowIndex);
  if (row == null) {
   row = sheet.createRow(rowIndex);
  }
  HSSFCell cell = row.getCell(colIndex);
  if (cell == null) {
   cell = row.createCell(colIndex);
  }
  return cell;
 }

//将行rowIndex1的格式复制到行rowIndex2

 public static void copySheetRowStyle(HSSFSheet sheet, int rowIndex1,
   int rowIndex2) {
  // check paras
  if(rowIndex1 == rowIndex2){
   return;
  }
  
  HSSFRow row = sheet.getRow(rowIndex1);
  if (row == null) {
   row = sheet.createRow(rowIndex1);
  }
  short cols = row.getLastCellNum();
  HSSFCellStyle style = null;
  for (short colIndex = 0; colIndex < cols; colIndex++) {
   style = ExcelUtil.getSheetCellStyle(sheet, rowIndex1, colIndex);
   ExcelUtil.setSheetCellStyle(sheet, rowIndex2, colIndex, style);
  }
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值