首页 > 学院 > 开发设计 > 正文

java生成excel

2019-11-14 15:28:22
字体:
来源:转载
供稿:网友
package test.poi;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;import java.util.Map;import jxl.Workbook;import jxl.format.Alignment;import jxl.format.Border;import jxl.format.BorderLineStyle;import jxl.format.Colour;import jxl.format.UnderlineStyle;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class createExcel {    public static void main(String[] args) {        createExcel();    }    public static void createExcel() {        try {            File file = new File("f://text.xls");            OutputStream os = new FileOutputStream(file);            WritableWorkbook wwb = Workbook.createWorkbook(os);            WritableSheet ws = wwb.createSheet("sheet1", 0);            //根据月份获取对象集            List<Map<String,String>> list=new ArrayList<Map<String,String>>();            //设置单元格格式            WritableFont wf = new WritableFont(WritableFont.ARIAL, 11,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.WHITE);            WritableCellFormat cellFormat = new WritableCellFormat(wf);            cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLUE_GREY);            cellFormat.setAlignment(Alignment.CENTRE);            cellFormat.setBackground(Colour.BLUE);            //第一行            Label label = new Label(0, 0, "序号");            label.setCellFormat(cellFormat);            ws.addCell(label);            label = new Label(1, 0, "姓名 ");            label.setCellFormat(cellFormat);            ws.addCell(label);            //合并单元格            ws.mergeCells(2, 0, 3, 0);            label = new Label(2, 0, "合并单元格");            label.setCellFormat(cellFormat);            ws.addCell(label);                                //第二行            label = new Label(0, 1, "1");            ws.addCell(label);            label = new Label(1, 1, "姓名");            ws.addCell(label);            label = new Label(2, 1, "合并1");            ws.addCell(label);            label = new Label(3, 1, "合并2");            ws.addCell(label);            //第三行            label = new Label(0, 2, "2");            ws.addCell(label);            label = new Label(1, 2, "姓名");            ws.addCell(label);            label = new Label(2, 2, "合并1");            ws.addCell(label);            label = new Label(3, 2, "合并2");            ws.addCell(label);            //添加数据            wwb.write();            wwb.close();            System.out.PRintln("生成信息表(Excel格式)成功");        } catch (Exception e) {            System.out.println("生成信息表(Excel格式)时出错:");            e.printStackTrace();        }    }}

操作excel需用到jxl.jar这个jar包。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表