JXL API总结

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总结

上一篇:mysql数据库_serialisers


下一篇:MySQL 8.0.20 安装教程图文详解(windows 64位)