poi 生成Excel 并保存到 本地

jar包

               <dependency>

    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
    <version>1.0.6</version>
</dependency>

public void findOrganAccountToExcel() {

Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String ds = formatter.format(date);

List<VenusOrgan>  list = dao.findOrganByLastupdtimeAndtxstate(ds);
 
OutputStream out = null;

// 创建一个excel文件
HSSFWorkbook book = new HSSFWorkbook();
// 创建Sheet对象
HSSFSheet sheet = book.createSheet("机构客户信息表");
if (list != null && list.size()>0) {

// 创建excel文件标题
String[] tableHeader = { "客户编号", "分支代码", "经办人"};

HSSFRow  row = null;
HSSFCell cell = null;
int rowIndex = 2;
row = sheet.createRow(1);
int i =0;
for (String string : tableHeader) {
cell = row.createCell(i);
cell.setCellValue(string);
i++;
}

for (VenusOrgan organ : list) {
row = sheet.createRow(rowIndex);
cell = row.createCell(0);
cell.setCellValue(organ.getOrgan_id());

cell = row.createCell(1);
cell.setCellValue(organ.getBranch_no());

cell = row.createCell(2);
cell.setCellValue(organ.getOperator_no());

cell = row.createCell(3);
cell.setCellValue(organ.getOrgan_name());

rowIndex++;
}

}

 File file=new File("d:\\poi\\");
         OutputStream stream=null;
         try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
            stream = new FileOutputStream(new File(file, "机构客户信息表_"+sdf.format(date)+".xls"));
             //document.write(stream);
             book.write(stream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(stream != null);
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

猜你喜欢

转载自my.oschina.net/hfq/blog/1817770