jxl框架

//Create Excle workbook
public boolean writeToSheet(int id) throws SQLException,IOException
{
//Write to sheet
try{
if (this.copyFile(pathFrom,pathTo))
{
String filename = pathTo;
short rownumber = 1;
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename + "/Student.xls"));  
            HSSFWorkbook wb  = new HSSFWorkbook(fs);  
            HSSFSheet sheet = wb.getSheetAt(0);
            ArrayList arrstu = (ArrayList)Student.getAllStudents(id);
           
            //Write to sheet
            for(int j=0; j < arrstu.size();j++)
            {
            Student stu = (Student)arrstu.get(j);
           //Get Row
            HSSFRow row =  sheet.getRow(rownumber);  
                if (row == null)
                {
                  row =sheet.createRow(rownumber);
                }
                //Get Name cell
                HSSFCell cellname = row.getCell((short)0);  
                if(cellname == null)
                {
                cellname   =   row.createCell((short)0);  
                }
                cellname.setCellType(HSSFCell.CELL_TYPE_STRING);  
                cellname.setCellValue(stu.getStuname());
               
                //Get Number cell
                HSSFCell cellnum = row.getCell((short)1);  
                if(cellnum == null)
                {
                cellnum   =   row.createCell((short)1);  
                }
                cellnum.setCellType(HSSFCell.CELL_TYPE_STRING);  
                cellnum.setCellValue(stu.getStunum());
               
                //Get Age Cell
                HSSFCell cellage = row.getCell((short)2);  
                if(cellage == null)
                {
                cellage   =   row.createCell((short)2);  
                }
                cellage.setCellType(HSSFCell.CELL_TYPE_STRING);  
                cellage.setCellValue(String.valueOf(stu.getStuage()));
               
                //Get Sex Cell
                HSSFCell cellsex = (HSSFCell)row.getCell((short)3);
                if(cellsex == null)
                {
                cellsex   =   row.createCell((short)3);  
                }
                cellsex.setCellType(HSSFCell.CELL_TYPE_STRING);
                //Male
if(stu.getStusex().equals("1"))
{
cellsex.setCellValue("MALE");
}
else
{
cellsex.setCellValue("FAMALE");
}

//Get Information Cell
HSSFCell cellinfo = row.getCell((short)4);  
                if(cellinfo == null)
                {
                cellinfo   =   row.createCell((short)4);  
                }
                cellinfo.setCellType(HSSFCell.CELL_TYPE_STRING);  
                cellinfo.setCellValue(stu.getStuinfo());
               
                rownumber++;
            }
            //   Write   the   output   to   a   file  
            FileOutputStream fileOut = new FileOutputStream(filename + "/Student.xls");  
            wb.write(fileOut);  
            fileOut.close();
return true;

}
}catch (Exception ex)
        {
System.out.println("Write sheet error");
        System.out.println(ex.getMessage());
        }
return false;
}
=====================================================
这是我曾经写过的一个用POI 操作excel的例子,里面的思路你看一下,至于那些方法和类和jxl得没区别,而且jxl得更简单

【KimmKing】:
import jxl.*;
import jxl.write.*;
import java.io.*;
import java.io.File.*;
import java.util.*;



public class excel
{
public static void main(String[] args)
{

String targetfile = "c:/out.xls";//输出的excel文件名
String worksheet = "List";//输出的excel文件工作表名
String[] title = {"ID","NAME","DESCRIB"};//excel工作表的标题


WritableWorkbook workbook;
try
{
//创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下
//workbook = Workbook.createWorkbook(new File("output.xls"));
System.out.println("begin");

OutputStream os=new FileOutputStream(targetfile);
workbook=Workbook.createWorkbook(os);

WritableSheet sheet = workbook.createSheet(worksheet, 0); //添加第一个工作表
//WritableSheet sheet1 = workbook.createSheet("MySheet1", 1); //可添加第二个工作
/*
jxl.write.Label label = new jxl.write.Label(0, 2, "A label record"); //put a label in cell A3, Label(column,row)
sheet.addCell(label);
*/

jxl.write.Label label;
for (int i=0; i<title.length; i++)
{
//Label(列号,行号 ,内容 )
label = new jxl.write.Label(i, 0, title[i]); //put the title in row1
sheet.addCell(label);
}




//下列添加的对字体等的设置均调试通过,可作参考用


//添加数字
jxl.write.Number number = new jxl.write.Number(3, 4, 3.14159); //put the number 3.14159 in cell D5
sheet.addCell(number);

//添加带有字型Formatting的对象
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES,10,WritableFont.BOLD,true);
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
jxl.write.Label labelCF = new jxl.write.Label(4,4,"文本",wcfF);
sheet.addCell(labelCF);

//添加带有字体颜色,带背景颜色 Formatting的对象
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
wcfFC.setBackground(jxl.format.Colour.BLUE);
jxl.write.Label labelCFC = new jxl.write.Label(1,5,"带颜色",wcfFC);
sheet.addCell(labelCFC);

//添加带有formatting的Number对象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
jxl.write.Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
sheet.addCell(labelNF);

//3.添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0,2,false);
sheet.addCell(labelB);

//4.添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
sheet.addCell(labelDT);

//添加带有formatting的DateFormat对象
jxl.write.DateFormat df = new jxl.write.DateFormat("ddMMyyyyhh:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1,3,new java.util.Date(),wcfDF);
sheet.addCell(labelDTF);

//和宾单元格
//sheet.mergeCells(int col1,int row1,int col2,int row2);//左上角到右下角
sheet.mergeCells(4,5,8,10);//左上角到右下角
wfc = new jxl.write.WritableFont(WritableFont.ARIAL,40,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
jxl.write.WritableCellFormat wchB = new jxl.write.WritableCellFormat(wfc);
wchB.setAlignment(jxl.format.Alignment.CENTRE);
labelCFC = new jxl.write.Label(4,5,"单元合并",wchB);
sheet.addCell(labelCFC); //


//设置边框
jxl.write.WritableCellFormat wcsB = new jxl.write.WritableCellFormat();
wcsB.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THICK);
labelCFC = new jxl.write.Label(0,6,"边框设置",wcsB);
sheet.addCell(labelCFC);
workbook.write();
workbook.close();
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println("end");
Runtime r=Runtime.getRuntime();
Process p=null;
//String cmd[]={"notepad","exec.java"};
String cmd[]={"C:\\Program Files\\Microsoft Office\\Office\\EXCEL.EXE","out.xls"};
try{
p=r.exec(cmd);
}
catch(Exception e){
System.out.println("error executing: "+cmd[0]);
}


}
}

【KimmKing】:
要往xls文件里面写入数据的时候需要注意的是第一要新建一个xls文件
OutputStream os=new FileOutputStream("c:excel2.xls");

再建完这个文件的时候再建立工作文件
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(os));

如果这个文件已经存在,那么我们可以在这个文件里面加入一个sheet为了和以前的数据进行分开;
jxl.write.WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
在createSheet方法里前面的参数是sheet名,后面是要操作的sheet号

接下来就可以往这个文件里面写入数据了


写入数据的时候注意的格式


(1)添加的字体样式
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
WritableFont()方法里参数说明:
这个方法算是一个容器,可以放进去好多属性
第一个: TIMES是字体大小,他写的是18
第二个: BOLD是判断是否&Icirc;±体,选择true时&Icirc;±体
第三个: ARIAL
第四个: UnderlineStyle.NO_UNDERLINE 下划线
第五个: jxl.format.Colour.RED 字体颜色是红色的

jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);

jxl.write.Label labelC = new jxl.write.Label(0, 0, "This is a Label cell",wcfF);
ws.addCell(labelC);
在Label()方法里面有三个参数
第一个是代表列数,
第二是代表行数,
第三个代表要写入的内容
第四个是可选项,是输入这个label里面的样式
然后通过写sheet的方法addCell()把内容写进sheet里面。

(2)添加带有formatting的Number对象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");


(3)添加Number对象
(3.1)显示number对象数据的格式

jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);

jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);
Number()方法参数说明:
前两上表示输入的位置
第三个表示输入的内容


(4)添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
ws.addCell(labelB);


(5)添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
ws.addCell(labelDT);
DateTime()方法的参数说明
前两个表示输入的位置
第三个表示输入的当前时间


(6)添加带有formatting的DateFormat对象
这个显示当前时间的所&Oacute;行畔&cent;,包括年月日小时分秒
jxl.write.DateFormat df = new jxl.write.DateFormat("dd MM yyyy hh:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1, 3, new java.util.Date(), wcfDF);
ws.addCell(labelDTF);

(7)添加带有字体颜色Formatting的对象
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);

import="jxl.format.*
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,20,WritableFont.BOLD,false,
UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);

(8)设置单元格样式

jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
wcfFC.setBackGround(jxl.format.Colour.RED);//设置单元格的颜色为红色
wcfFC = new jxl.write.Label(6,0,"i love china",wcfFC);

你可能感兴趣的:(java,Excel)