用jxl导出数据到excel

需要jxl.jar

测试结果没问题,代码:

package com;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExportExcel {
    
    public void exportExcel(String exportPath){
        File file = new File(exportPath);
        if(!file.exists()||file.isDirectory()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        WorkbookSettings WorkbookSettings = new WorkbookSettings();
        WorkbookSettings.setEncoding("gbk");
        
        WritableWorkbook writableWorkbook = null;
        try {
            writableWorkbook = Workbook.createWorkbook(file, WorkbookSettings);
            
            List<List<String>> data = getData();
            
            int dataSize = data.size();
            
            int maxItem = 10;//每个sheet最多能添加10条记录
            
            int totalSheetCount = (dataSize/maxItem)+1;//总sheet数
            
            for(int sheetIndex=0;sheetIndex<totalSheetCount;sheetIndex++){
                WritableSheet sheet = writableWorkbook.createSheet("Sheet"+(sheetIndex+1), sheetIndex);
                List<String> titles = data.get(0);
                
                WritableCellFormat writableCellFormat = new WritableCellFormat();//格式 TODO
                
                //添加title
                for(int columnIndex=0;columnIndex<titles.size();columnIndex++){
                    String title = titles.get(columnIndex);
                    
                    try {
                        sheet.addCell(new Label(columnIndex, 0, title, writableCellFormat));//因为放在第一行,所以第二个参数(行Index)默认成0
                    } catch (RowsExceededException e) {
                        e.printStackTrace();
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }
                }
                
                for(int rowIndex = sheetIndex * maxItem + 1;rowIndex<(sheetIndex * maxItem + maxItem);rowIndex++){
                    
                    int dataIndex = rowIndex-sheetIndex;//数据对应的index号
                    List<String> items = data.get(dataIndex);//每行内容
                    
                    for(int columnIndex=0;columnIndex<items.size();columnIndex++){
                        String content = items.get(columnIndex);//每个cell中的内容
                        
                        try {
                            sheet.addCell(new Label(columnIndex,rowIndex-(sheetIndex * maxItem), content, writableCellFormat));
                        } catch (RowsExceededException e) {
                            e.printStackTrace();
                        } catch (WriteException e) {
                            e.printStackTrace();
                        }
                    }
                    
                    if((dataIndex+1)==dataSize){//最后一条数据完成后终止
                        break;
                    }
                    
                }
                
            }
            
            writableWorkbook.write();

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(writableWorkbook!=null){
                try {
                    writableWorkbook.close();
                } catch (WriteException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            
        }
        
    }
    
    /**
     * 数据源
     * @return
     */
    public List<List<String>> getData(){
        List<List<String>> data = new ArrayList<List<String>>();
        
        List<String> titles = new ArrayList<String>();
        titles.add("序号");
        titles.add("姓名");
        titles.add("性别");
        titles.add("生日");
        titles.add("电话");
        titles.add("邮箱");
        
        data.add(titles);
        for(int i=0;i<20;i++){
            List<String> contents = new ArrayList<String>();
            contents.add((i+1)+"");
            contents.add("测试人员"+(i+1));
            contents.add("男");
            contents.add("1990-01-01");
            contents.add("1851515151"+(i+1));
            contents.add("123@12"+(i+1)+".com");
            
            data.add(contents);
        }

        
        return data;
    }
    
    public static void main(String[] args) {
        String exportPath = "D:\exportExcel.xls";
        ExportExcel exportExcel = new ExportExcel();
        exportExcel.exportExcel(exportPath);;
    }
}
原文地址:https://www.cnblogs.com/jinzhiming/p/5896747.html