关闭

导出到Excel

679人阅读 评论(0) 收藏 举报

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:91933次
    • 积分:1354
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:36篇
    • 译文:1篇
    • 评论:27条
    最新评论