oracle 写入excel

来源:互联网 发布:无损音乐刻录软件 编辑:程序博客网 时间:2024/05/18 14:23

 

//下面这段代码是将内容写入一个excel文档
//----------------------------------------------------------------------
package Print;
import java.util.StringTokenizer;
import jxl.*;
import jxl.write.*;
import jxl.format.*;
import java.io.*;
import java.sql.*;
import conn.*;
import TypeChare.*;
public class writeexcel
{
  WritableSheet sheet;
  File tempFile;
  WritableWorkbook workbook;

WritableFont headerFont;
  WritableFont titleFont;
WritableFont detFont;
WritableCellFormat titleFormat;
  WritableCellFormat headerFormat;
WritableCellFormat detFormat;
WritableCellFormat priceFormat;
  WritableCellFormat dateFormat;

  Label l=null;
jxl.write.Number n=null;
jxl.write.DateTime d=null;

  int column=0;
//================== 打开EXCEL文件 =============================
public writeexcel(String OutFile)
{
  try{
tempFile=new File(OutFile);
workbook = Workbook.createWorkbook(tempFile);
sheet = workbook.createSheet("用户", 0);

   headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
headerFormat = new WritableCellFormat (headerFont);

titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
titleFormat = new WritableCellFormat (titleFont);

detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
detFormat = new WritableCellFormat (detFont);

NumberFormat nf=new NumberFormat("0.00"); //用于Number的格式
priceFormat = new WritableCellFormat (detFont, nf);

DateFormat df=new DateFormat("yyyy-MM-dd");//用于日期的
dateFormat = new WritableCellFormat (detFont, df);

   //合并第一列第一行到第六列第一行的所有单元格
   sheet.mergeCells(0,0,5,0);
   //单元格边框设置
   headerFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.MEDIUM);
   titleFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
   detFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
   priceFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
   dateFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
   //单元格文字位置设置
   headerFormat.setAlignment(jxl.format.Alignment.CENTRE);

  }
  catch(Exception e)
{
System.out.println("error0:"+e.getMessage());
}
}
//======================= 显示表头 ================================
public boolean setColumTitle(String Columtitle)
  {
//表头
  try{
l=new Label(0, 0, Columtitle, headerFormat);
sheet.addCell(l); }
  catch(Exception e)
{
System.out.println("error1:"+e.getMessage());
}
   return true;
  }
//======================= 显示表内容 =============================
public boolean DisplayDetail(String sql)
  {

   try{

   int h=0;
   conn conn= new conn();
ResultSet Rs=conn.executeQuery(sql);

   column=0;
for (int i=1; i<=Rs.getMetaData().getColumnCount(); i++){
l=new Label(column++, 2, Rs.getMetaData().getColumnName(i), titleFormat);
//sheet.setColumnView(column,Rs.getString(i).length());
   //System.out.println(Rs.getMetaData().getColumnDisplaySize(i));
sheet.addCell(l);}
while (Rs.next()){
     column=0;
for (int j=1; j<=Rs.getMetaData().getColumnCount(); j++){
       if (Rs.getMetaData().getColumnTypeName(j).compareTo("VARCHAR2")==0)
l=new Label(column++, h+3, Rs.getString(j),detFormat);
       if (Rs.getMetaData().getColumnTypeName(j).compareTo("NUMBER")==0)
l=new Label(column++, h+3, Rs.getString(j),priceFormat);
       if (Rs.getMetaData().getColumnTypeName(j).compareTo("DATE")==0)
l=new Label(column++, h+3, Rs.getString(j),dateFormat);
       sheet.addCell(l);
     }
      h++;
   }
Rs=null;
conn.close();

   //设置列的宽度
//column=0;
//sheet.setColumnView(column++, 10);
//sheet.setColumnView(column++, 10);
//sheet.setColumnView(column++, 10);
//sheet.setColumnView(column++, 10);

workbook.write();
workbook.close();
   }
   catch(Exception e)
{
System.out.println("error1:"+e.getMessage());
}
   return true;
  }

public static void main(String args[])
{
// new writeexcel("d:/Print.xls");
}
}