当前位置:软件学习 > Excel >>

java Jxl 操作Excel

1.首先,创建一个可读写的工作簿(WritableWorkbook):
[java]
WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test.xls"));   

2.如果是想要修改一个已存在的excel工作簿,则需要先获得它的原始工作簿,再创建一个可读写的副本:
[java]
Workbook wb = Workbook.getWorkbook(new File("src\\test\\test.xls")); // 获得原始文档    
WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\test_modified.xls"),wb); // 创建一个可读写的副本   

3.取得要操作的sheet,并对其进行相应的操作,如改名、合并单元格、设置列宽、行高等:
[java]
sheet.mergeCells(0, 0, 4, 0); // 合并单元格    
sheet.setRowView(0, 600); // 设置行的高度    
sheet.setColumnView(0, 30); // 设置列的宽度    
sheet.setColumnView(1, 20); // 设置列的宽度   

4.通过WritableFont、WritableCellFormat等对象可以设置单元格的字体、样式等外观:
[java]
WritableSheet sheet = workbook.getSheet(0);   WritableFont titleWf = new WritableFont(WritableFont.createFont("仿宋_GB2312"),// 字体    
                            20,//WritableFont.DEFAULT_POINT_SIZE,   // 字号    
                            WritableFont.NO_BOLD,                  // 粗体    
                            false,                                 // 斜体    
                            UnderlineStyle.NO_UNDERLINE,            // 下划线    
                            Colour.BLUE2,                       // 字体颜色    
                            ScriptStyle.NORMAL_SCRIPT);    
WritableCellFormat wcf = new WritableCellFormat(titleWf);    
wcf.setBackground(Colour.GRAY_25);// 设置单元格的背景颜色    
wcf.setAlignment(Alignment.CENTRE); // 设置对齐方式    
wcf.setBorder(Border.ALL, BorderLineStyle.THICK); // 添加边框    
cell.setCellFormat(wcf);   
 
sheet.setName("修改后"); // 给sheet页改名    
workbook.removeSheet(2); // 移除多余的标签页    
workbook.removeSheet(3);    
 
demo1:
[java]
public String createExcel(String path,String filename,List list,TblKepuoaRecordDispatch dispatch) throws Exception{ 
        File file = new File(path+File.separator+filename+".xls" ); 
         WritableWorkbook wbook = null; 
        try { 
            wbook = Workbook.createWorkbook(file); 
         // 建立excel文件 
            String tmptitle = filename; // 标题 
            WritableSheet wsheet = wbook.createSheet(filename, 0); // sheet名称 
              // 设置excel标题 
            WritableFont wfont = new WritableFont(WritableFont.createFont("宋体"), 16, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK); 
            WritableCellFormat wcfFC = new WritableCellFormat(wfont); 
            wcfFC.setAlignment(Alignment.CENTRE); // 设置对齐方式  
            wsheet.mergeCells(0, 0, 9, 0); // 合并单元格   
             
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC)); 
             
            wfont = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 13,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK); 
            wcfFC = new WritableCellFormat(wfont); 
            wcfFC.setBorder(Border.NONE,BorderLineStyle.NONE);  
            wcfFC.setAlignment(Alignment.CENTRE); // 设置对齐方式  
             
            wsheet.setColumnView(0, 6); // 设置列的宽度 
            wsheet.setColumnView(1, 20); // 设置列的宽度 
            wsheet.setColumnView(2, 25); // 设置列的宽度 
            wsheet.setColumnView(3, 60); // 设置列的宽度 
            wsheet.setColumnView(4, 10); // 设置列的宽度 
            wsheet.setColumnView(5, 6); // 设置列的宽度 
            wsheet.setColumnView(6, 6); // 设置列的宽度 
            wsheet.setColumnView(7, 80); // 设置列的宽度 
            wsheet.setColumnView(8, 20); // 设置列的宽度 
            wsheet.setColumnView(9, 20); // 设置列的宽度 
              // 开始生成主体内容                 
            wsheet.addCell(new Label(0, 1, "序号",wcfFC)); 
      

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,