免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1055 | 回复: 0
打印 上一主题 下一主题

Java操作Excel,Word [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-30 17:13 |只看该作者 |倒序浏览

Java操作Excel,Word
今天听朋友介绍,了解了POI, apache组织的一个开源项目,提供了对Microsoft excel,word 的纯java
解决方案,
http://jakarta.apache.org/poi/
  里面可以下载到开发包,还有例子程序,用起来还是很方便的!
下面是一个将Oracle数据库表导出为Excel的程序:
import java.io.FileOutputStream;
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.HSSFHeader;    //头   
import org.apache.poi.hssf.usermodel.HSSFRow;       //行
import org.apache.poi.hssf.usermodel.HSSFSheet;     //一张记事簿
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  //一个excel
import org.apache.poi.hssf.util.HSSFColor;          //颜色
import java.sql.*;
  
public class Test{
     
     public static void main(String[] args) throws  Exception
     {
       String filename = "dept.xls";
       String column_name="";
       PreparedStatement ps=null;  //预编译
       Connection conn=null;      //连接
       Statement smt=null;         //sql语句
       ResultSet rs = null,rs1=null;        //结果集合
       ResultSetMetaData md = null;         
      long rowCount =0l;               //总行数
        try{
      
        Class.forName("oracle.jdbc.driver.OracleDriver");
        }catch(Exception e){
         e.printStackTrace();
        }
        String url = "jdbc:oracle:thin:@172.18.2.114:1521:MIS";
        String userName = "scott";
        String password = "tiger";
        conn = DriverManager.getConnection (url, userName, password);
        smt = conn.createStatement();
        
        //取得总记录数
        String sql="select deptno,dname,loc from dept";
        String sqlqeury="select count(*) as total from (  "+sql+"  )";
        try{
             rs = smt.executeQuery(sqlqeury);
             if(rs.next()){
                 rowCount =rs.getLong("total");
                 System.out.print(rowCount) ; //记录总数
              }
          }catch(Exception e){
           System.out.print("aaa");
                 e.printStackTrace();
                 System.out.println(e.getMessage());
                 conn.close();   //异常后关闭连接
                 throw new Exception("获得记录总数失败");
           }
         
         //处理sql语句
         try{
              rs1 = smt.executeQuery(sql);
              md = rs1.getMetaData();
               System.out.print(md.getColumnCount());
          }catch(Exception e){
            System.out.print("bbb");
                e.printStackTrace();
                System.out.println(e.getMessage());
                conn.close();   //异常后关闭连接
                throw new Exception("传入的SQL无法处理");
         }
         
         
        //生成excel代码
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFRow row = null;
        HSSFCell cell=null;
        for(long i =0;i<=rowCount;i++){
            
            row = sheet.createRow((short)i);                     //创建一个行
            for(int j =0;j<md.getColumnCount();j++){
            
               cell = row.createCell((short)j);                  //创建单元格
               cell.setEncoding(HSSFCell.ENCODING_UTF_16);
               if(i==0){   
                  cell.setCellValue(md.getColumnName(j+1));     //设定单元格的值
               }else{
                  cell.setCellValue(rs1.getString(j+1));        //设定单元格的值
                }
             }
             rs1.next();
         }
        //写到excel中!
        FileOutputStream fileOut = new FileOutputStream(filename);
        wb.write(fileOut);          //向fileout文件写
        fileOut.close();            //关闭文件输出流
        conn.close();               //关闭正常连接   
   }
   
}
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15317/showart_268357.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP