Chinaunix首页 | 论坛 | 博客
  • 博客访问: 76713
  • 博文数量: 17
  • 博客积分: 570
  • 博客等级: 中士
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-15 20:08
文章分类

全部博文(17)

文章存档

2009年(4)

2008年(3)

2007年(2)

2006年(8)

我的朋友

分类: Java

2008-08-03 12:20:57


参考http://blog.csdn.net/jinjazz/archive/2008/08/01/2753869.aspx
我来个JAVA版本的,
不过writeNumber上实现上有一点点瑕疵;也没找到解决办法,只要调用了writeString
同时增加addBean,addLine方法增强了这个类的功能!
在做简单的EXCEL导出功能从此可摆脱jxl!
 
 

import java.io.*;
import java.util.*;


public class ExcelWriter {
    public static void main(String args[]){
        try {
            ExcelWriter excel = new ExcelWriter("c://mytest.xls");
             excel.beginWrite();
    
     String head[] = {"StuNumber","Name","Score"};
     excel.addLine(head);
    
     List<String> list = new ArrayList<String>();
     list.add("99");
     list.add("jinjazz");
     list.add("99.9");
     excel.addLine(1,list);
    
     java.util.List<GradePO> gradeList = new ArrayList<GradePO> ();
     for(int i=0;i < 10 ; i++){
         GradePO grade = new GradePO();
         grade.setStuNumber(i);
         grade.setName("学生"+i);
         grade.setScore(88.8f + i);
         gradeList.add(grade);
     }
     String fields[] = {"stuNumber","name","score"};
     excel.addBean(gradeList, fields);
    
     excel.writeNumber(12, 0, 12);
     excel.writeString(12, 1, "ypzhuang");
     excel.writeNumber(12, 2, 100.0d);
     excel.endWrite();
    
     System.out.println("write file ok");
        } catch (FileNotFoundException e) {
            System.err.print(e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.print(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.print(e.getMessage());
            e.printStackTrace();
        }
    }
    private FileOutputStream _wirter;
    private int row = 0;
    private String path;

    public ExcelWriter(String strPath) throws FileNotFoundException {
     _wirter = new FileOutputStream(strPath);
     path = strPath;
    }

    /**
     * 写入short数组
     * @param values
     * @throws IOException
     */

    private void _writeFile(short[] values) throws IOException {
        for (short v : values) {
            byte[] b = getBytes(v);
            _wirter.write(b, 0, b.length);
        }
    }

    /**
     * 写文件头
     * @throws IOException
     */

    public void beginWrite() throws IOException {
        _writeFile(new short[] { 0x809, 8, 0, 0x10, 0, 0 });
    }

    /**
     * 写文件尾
     * @throws IOException
     */

    public void endWrite() throws IOException {
        _writeFile(new short[] { 0xa, 0 });
        _wirter.close();
    }

    /**
     * 写一个浮点数到单元格x,y
     * @param x
     * @param y
     * @param value
     * @throws IOException
     */

    public void writeNumber(short x, short y, float value) throws IOException {
//        _writeFile(new short[] { 0x203, 14, x, y, 0 });

//        byte[] b = getBytes(value);

//        _wirter.write(b, 0, b.length);

        writeString((short)x,(short)y,value+"");
    }
    
    /**
     * 写一个数字到单元格x,y
     * @param x
     * @param y
     * @param value
     * @throws IOException
     */

    public void writeNumber(int x, int y, float value) throws IOException {
        writeNumber((short)x,(short)y,value);
    }

    /**
     * 写一个字符到单元格x,y
     * @param x
     * @param y
     * @param value
     * @throws IOException
     */

    public void writeString(short x, short y, String value) throws IOException {
        byte[] b = getBytes(value);
        _writeFile(new short[] { 0x204, (short) (b.length + 8), x, y, 0,(short) b.length });
        _wirter.write(b, 0, b.length);
    }
    
    /**
     * 写一个字符到单元格x,y
     * @param x
     * @param y
     * @param value
     * @throws IOException
     */

    public void writeString(int x, int y, String value) throws IOException {
        writeString((short)x,(short)y,value);
    }


    /**
     * 写一个整数到单元格x,y
     * @param x
     * @param y
     * @param value
     * @throws IOException
     */

    public void writeNumber(short x, short y, int value) throws IOException {
//        _writeFile(new short[] { 0x203, 14, x, y, 0 });

//        byte[] b = getBytes(value);

//        _wirter.write(b, 0, b.length);

        writeString(x,y,value+"");
    }
    
    /**
     * 写一个整数到单元格x,y
     * @param x
     * @param y
     * @param value
     * @throws IOException
     */

    public void writeNumber(int x, int y, int value) throws IOException {
        writeNumber((short)x,(short)y,value);
    }
    
    /**
     * 写一个双精度浮点数到单元格x,y
     * @param x
     * @param y
     * @param value
     * @throws IOException
     */

    public void writeNumber(short x, short y, double value) throws IOException {
        writeString(x,y,value+"");
    }
    
    /**
     * 写一个双精度浮点数到单元格x,y
     * @param x
     * @param y
     * @param value
     * @throws IOException
     */

    public void writeNumber(int x, int y, double value) throws IOException {
        writeNumber((short)x,(short)y,value);
    }
    
    /**
     * row行写入一行字符串
     * @param rows
     * @param head
     * @throws IOException
     */

    public void addLine(int rows,String head[]) throws IOException{
        if(rows < 0){
            rows = 0;
        }
        for(int i=0;head!=null && i < head.length;i++){
            writeString(rows,i,head[i]);            
        }
        row = rows+1;
    }
    
    /**
     * 在第0行写入一行字符串
     * @param head 字符数组
     * @throws IOException
     */

    public void addLine(String head[]) throws IOException{        
        addLine(0,head);            
    }
    
    /**
     * 在row行写入一行字符串
     *
     * @param rows
     * @param list 字符LIST
     * @throws IOException
     */

    public void addLine(int rows,java.util.List<String> list) throws IOException{
        if(rows < 0){
            rows = 0;
        }    
        for(int i=0;list!=null && i<list.size();i++){
            writeString(rows,i,list.get(i));    
        }
        row = rows + 1;
    }
    
    /**
     * 在当前行写入一行字符串
     * @param list
     * @throws IOException
     */

    public void addLine(java.util.List<String> list) throws IOException{
        addLine(row,list);        
    }
    
    /**
     * 在当前行开始写入JavaBean对象List
     * @param beans
     * @param fields
     * @throws Exception
     */

    public void addBean(java.util.List beans, String fields[]) throws Exception{
        
        String methodName = null;
        Object params[] = new Object[0];
        Class paramCls[] = new Class[0];
        
        List<String> list = new ArrayList<String>();
        for (Iterator iterator = beans.iterator(); iterator.hasNext();) {
            Object obj = iterator.next();
            int l = fields.length;
            for (int j = 0; j < l; j++) {
                String field = fields[j];                
                methodName = (new StringBuilder("get")).append(
                        field.substring(0, 1).toUpperCase()).append(
                        field.substring(1)).toString();
                String tmp = String.valueOf(obj.getClass().getMethod(methodName, paramCls).invoke(obj, params));
                list.add(tmp);
                            
            }
            addLine(list);
            list.clear();
        }
        
    }

    private byte[] getBytes(short n) {
        byte[] b = new byte[2];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        return b;
    }
     
    private byte[] getBytes(int n) {
//        byte[] b = new byte[4];

//

//        b[0] = (byte) (n & 0xff);

//        b[1] = (byte) (n >> 8 & 0xff);

//        b[2] = (byte) (n >> 16 & 0xff);

//        b[3] = (byte) (n >> 24 & 0xff);


//         b[3] = (byte) (n & 0xff);

//         b[2] = (byte) (n >> 8 & 0xff);

//         b[1] = (byte) (n >> 16 & 0xff);

//         b[0] = (byte) (n >> 24 & 0xff);

//         return b;

//         return getBytes((short)n);

        return getBytes(n + "");
    }

    private byte[] getBytes(float f) {
        return getBytes(Float.floatToRawIntBits(f));
    }

    private byte[] getBytes(String s) {        
        return s.getBytes();
    }
    
    public InputStream getInputStreamResult() throws IOException {
         if(_wirter !=null){
             endWrite();
         }
        return new FileInputStream(path);
    }
    
    
}

 
用到的PO
 


public class GradePO{
    private int stuNumber;
    private String name;
    private float score;
    public int getStuNumber() {
        return stuNumber;
    }
    public void setStuNumber(int stuNumber) {
        this.stuNumber = stuNumber;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getScore() {
        return score;
    }
    public void setScore(float score) {
        this.score = score;
    }
    
}

阅读(1074) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~