导出到Excel

import java.io.FileOutputStream;
import java.util.List;

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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.sinosoft.utility.ExeSQL;


public class CreateReceiptClassImportTemplate {

 
    public boolean CrtReceiptClassImportTemplate(List list,
                                                 String filename,String sheetname,String interfacetype) {
     
        System.out.println("====filename=in=java="+filename);
      
        HSSFWorkbook wb = new HSSFWorkbook(); //建立新HSSFWorkbook对象
     
        HSSFSheet sheet = wb.createSheet();
        wb.setSheetName( 0, sheetname, HSSFWorkbook.ENCODING_UTF_16 );
        sheet.setDefaultColumnWidth((short) 15);

        //样式2    宋体  写正文时用
        HSSFFont font2 = wb.createFont();
        font2.setColor(HSSFFont.COLOR_NORMAL);
        font2.setFontName("宋体");
        font2.setFontHeightInPoints((short) 12);
        HSSFCellStyle normal = wb.createCellStyle();
        normal.setFont(font2);
        normal.setAlignment(HSSFCellStyle.ALIGN_LEFT); //水平居中
        normal.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP); //垂直居中
        normal.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
        HSSFRow row;
        HSSFCell cell;
        HSSFCell csCell;

        //行
        int i = 0;
    
  
         row = sheet.createRow((short) i);
         cell = row.createCell((short) (0)); //建立新cell
         cell.setCellStyle(normal);
         cell.setEncoding(HSSFCell.ENCODING_UTF_16); //设置cell编码解决中文高位字节截断
         cell.setCellValue(interfacetype); //设置中西文结合字符串
       
         row = sheet.createRow((short) 1);
         for (int tmpi=1;tmpi<=list.size();tmpi++)
         {
          try{
            
             cell = row.createCell((short) (tmpi-1)); //建立新cell
             cell.setCellStyle(normal);
             cell.setEncoding(HSSFCell.ENCODING_UTF_16); //设置cell编码解决中文高位字节截断
             cell.setCellValue((String)list.get(tmpi)); //设置中西文结合字符串
            
          }
          catch(Exception e)
          {
          System.out.println(e);
          return false;
          }
          }

         try{
                 FileOutputStream fileOut = new FileOutputStream(filename);
                 wb.write(fileOut);
                 fileOut.close();
                 System.out.println("====动态模板文件excel文件生成成功:========"+filename);
              }catch(Exception e1)
              {
                  return false;
              }
        return true;
    }
}

 

不要忘记加入Jar包poi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值