码迷,mamicode.com
首页 > Windows程序 > 详细

JXL API总结

时间:2015-02-10 15:11:46      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

API参考:

http://www.andykhan.com/jexcelapi/index.html

官网:

http://jexcelapi.sourceforge.net/

1. 创建可写的工作簿workbook

WritableWorkbook workbook = Workbook.createWorkbook(new File("c:\test.xls"));

2. 读取现有的工作簿workbook(不可写)

Workbook workbook = Workbook.getWorkbook(new File("c:\template.xls"));

3. 创建可写的Sheet

workbook.createSheet("name", 0);

4. 读取现有的Sheet

workbook.getSheet(0);

5. 创建Label

Label label = new Label(0,0,"content");

6. 将Label 添加到Sheet

sheet.addCell(label);

7. 根据模板创建workbook

Workbook template = Workbook.getWorkbook(new File("c:\template.xls"));
WritableWorkbook workbook = Workbook.createWorkbook(new File("c:\test.xls"),template);

8. 创建Excel的例子

WritableWorkbook workbook = Workbook.createWorkbook(new File("c:\test.xls"));
WritableSheet sheet = workbook.getSheet(0);
Label label1 = new Label(0,0,"content");
Label label2 = new Label(0,1,"content");
sheet.addCell(label1);
sheet.addCell(label2);
workbook.write();
workbook.close();

9. 根据模板创建Excel的例子

Workbook template = Workbook.getWorkbook(new File("c:\template.xls"));
WritableWorkbook workbook = Workbook.createWorkbook(new File("c:\test.xls"),template);
WritableSheet sheet = workbook.getSheet(0);
Label label1 = new Label(0,0,"content");
Label label2 = new Label(0,1,"content");
sheet.addCell(label1);
sheet.addCell(label2);
workbook.write();
workbook.close();

10. 使用模板单元格的格式

Workbook template = Workbook.getWorkbook(new File("c:\template.xls"));
WritableWorkbook workbook = Workbook.createWorkbook(new File("c:\test.xls"),template);
WritableSheet sheet = workbook.getSheet(0);    
//获取模板单元格的格式(字体,颜色等)    
CellFormat cellFormat = sheet.getWritableCell(0, 0).getCellFormat();
Label label = new Label(0,0,"content",cellFormat);
sheet.addCell(label);
workbook.write();
workbook.close();

11. 处理换行

WritableCellFormat wcf = new WritableCellFormat();
wcf.setWrap(true);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
Label label= new Label(0, 0,wcf);

12. struts2 下载excel

public InputStream getInputStream(){
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
                        Workbook template = Workbook.getWorkbook(new File("c:\template.xls"));
            WritableWorkbook workbook = Workbook.createWorkbook(os,template);
                WritableSheet sheet = workbook.getSheet(0);        
                CellFormat cellFormat = sheet.getWritableCell(0, 0).getCellFormat();
                Label label = new Label(0,0,"content",cellFormat);
                sheet.addCell(label);
                workbook.write();
                workbook.close();
                return new ByteArrayInputStream(os.toByteArray());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

JXL API总结

标签:

原文地址:http://www.cnblogs.com/tangyanbo/p/4283799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!