excel与java报表开发

来源:互联网 发布:iphone怎么传文件到mac 编辑:程序博客网 时间:2024/04/27 08:59
  1. import java.io.FileNotFoundException;   
  2. import java.io.FileOutputStream;   
  3. import java.io.IOException;   
  4. import java.sql.SQLException;   
  5.   
  6. import org.apache.poi.hssf.usermodel.HSSFCell;   
  7. import org.apache.poi.hssf.usermodel.HSSFRow;   
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  10. public class ExcelFile {   
  11.     /**  
  12.      * 写Excel操作  
  13.      * @param fileName  
  14.      * 文件名,文件要写入到的盘符和文件名,但不需要后缀名  
  15.      * @param fieldName  
  16.      * 表头名  
  17.      * @param res  
  18.      * 数据对象,java.sql.ResultSet  
  19.      */  
  20.     public static void writeExcel(String fileName,String [] fieldName,java.sql.ResultSet res)   
  21.     {   
  22.         FileOutputStream fos =null;   
  23.         try {   
  24.              fos = new FileOutputStream(fileName+".xls");   
  25.              HSSFWorkbook wb = new HSSFWorkbook();    
  26.              HSSFSheet s = wb.createSheet();    
  27.              createTag(fieldName,s);//写表格的头部   
  28.              createValue(res,s);//获取数据集,然后获得数据,写文件   
  29.              wb.write(fos);    
  30.              fos.close();    
  31.         } catch (FileNotFoundException e) {   
  32.             e.printStackTrace();   
  33.         } catch (IOException e) {   
  34.             e.printStackTrace();   
  35.         }   
  36.         finally  
  37.         {   
  38.             if(fos!=null)   
  39.             {   
  40.                 try {   
  41.                     fos.close();   
  42.                 } catch (IOException e) {   
  43.                     // TODO Auto-generated catch block   
  44.                     e.printStackTrace();   
  45.                 }   
  46.             }   
  47.         }   
  48.     }   
  49.        
  50.     /**  
  51.      * 创建表格表头  
  52.      * @param tags  
  53.      * @param s  
  54.      */  
  55.     private  static void createTag(String [] tags,HSSFSheet s)   
  56.     {   
  57.             HSSFRow row = s.createRow(0);    
  58.             HSSFCell cell = null;   
  59.             for(int i=0;i<tags.length;i++)   
  60.             {   
  61.                 cell = row.createCell(i);   
  62.                 cell.setCellValue(tags[i]);   
  63.             }   
  64.     }   
  65.        
  66.        
  67.     /**  
  68.      * 设置表格内容  
  69.      * @param res  
  70.      * @param s  
  71.      */  
  72.     private  static void createValue(java.sql.ResultSet res,HSSFSheet s)   
  73.     {   
  74.         try {   
  75.             int flag = 1;   
  76.             int count = res.getMetaData().getColumnCount();   
  77.             HSSFRow row = null;    
  78.             HSSFCell cell = null;   
  79.             while(res.next())   
  80.             {   
  81.                 row = s.createRow(flag);   
  82.                 for(int i=1;i<=count;i++)   
  83.                 {   
  84.                     cell = row.createCell(i-1);   
  85.                     Object obj = res.getObject(i);   
  86.                     cell.setCellValue(obj+"");   
  87.                 }   
  88.                 flag++;   
  89.             }   
  90.         } catch (SQLException e) {   
  91.             e.printStackTrace();   
  92.         }   
  93.   
  94.     }   
  95.   
  96. }  

test

 

 

 

  1. import com.jn.util.file.office.ExcelFile;   
  2.   
  3. public class ExcelFileText {   
  4.     public static void main(String [] args)   
  5.     {   
  6.         String sql = "select title_id,title,pub_id from titles";   
  7.         DBAccess dba = new DBAccess();   
  8.         ExcelFile.writeExcel("D://text",new String[]{"文章编号","文章主题","Pub_ID"},dba.query(sql));   
  9.         dba.closeConn();   
  10.     }   
  11. }  
原创粉丝点击