jxl操作EXCEL

这是一个Java程序,使用jxl库创建一个名为'产品清单'的Excel工作表,并填充数据,包括产品编号、名称、价格、数量、生产日期、产地和出口信息。程序还展示了如何设置单元格格式,如对齐方式、边框、背景色和字体样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
  1. package cztest;   
  2.   
  3. import java.io.FileOutputStream;   
  4. import java.io.OutputStream;   
  5. import java.text.SimpleDateFormat;   
  6. import java.util.Date;   
  7.   
  8. import jxl.Workbook;   
  9. import jxl.format.Alignment;   
  10. import jxl.format.Border;   
  11. import jxl.format.BorderLineStyle;   
  12. import jxl.format.CellFormat;   
  13. import jxl.write.Label;   
  14. import jxl.write.WritableCellFormat;   
  15. import jxl.write.WritableFont;   
  16. import jxl.write.WritableSheet;   
  17. import jxl.write.WritableWorkbook;   
  18.   
  19. public class JxlTest {   
  20.   
  21.     /**   
  22.      * @param args   
  23.      */     
  24.     public static void main(String[] args) {     
  25.          // 准备设置excel工作表的标题        
  26.         String[] title = {"编号","产品名称","产品价格","产品数量","生产日期","产地","是否出口"};        
  27.         try {        
  28.             // 获得开始时间        
  29.             long start = System.currentTimeMillis();        
  30.             // 输出的excel的路径        
  31.             String filePath = "c:\\test.xls";        
  32.             // 创建Excel工作薄        
  33.             WritableWorkbook wwb;        
  34.             // 新建立一个jxl文件,即在C盘下生成test.xls        
  35.             OutputStream os = new FileOutputStream(filePath);        
  36.             wwb=Workbook.createWorkbook(os);         
  37.             // 添加第一个工作表并设置第一个Sheet的名字        
  38.             WritableSheet sheet = wwb.createSheet("产品清单"0);        
  39.             Label label;        
  40.             for(int i=0;i<title.length;i++){        
  41.                 // Label(x,y,z)其中x代表单元格的第x+1列,第y+1行, 单元格的内容是y        
  42.                 // 在Label对象的子对象中指明单元格的位置和内容        
  43.                 label = new Label(i,0,title[i]);        
  44.                 // 将定义好的单元格添加到工作表中        
  45.                 sheet.addCell(label);        
  46.             }        
  47.             // 下面是填充数据        
  48.             /*      
  49.              * 保存数字到单元格,需要使用jxl.write.Number     
  50.              * 必须使用其完整路径,否则会出现错误     
  51.              * */       
  52.             // 填充产品编号        
  53.             jxl.write.Number number = new jxl.write.Number(0,1,20071001);        
  54.             sheet.addCell(number);        
  55.             // 填充产品名称        
  56.             label = new Label(1,1,"金鸽瓜子");        
  57.             sheet.addCell(label);        
  58.             /*     
  59.              * 定义对于显示金额的公共格式     
  60.              * jxl会自动实现四舍五入     
  61.              * 例如 2.456会被格式化为2.46,2.454会被格式化为2.45     
  62.              * */       
  63.             jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");        
  64.             jxl.write.WritableCellFormat wcf = new jxl.write.WritableCellFormat(nf);        
  65.             // 填充产品价格        
  66.             jxl.write.Number nb = new jxl.write.Number(2,1,2.45,wcf);        
  67.             sheet.addCell(nb);        
  68.             // 填充产品数量        
  69.             jxl.write.Number numb = new jxl.write.Number(3,1,200);        
  70.             sheet.addCell(numb);        
  71.             /*     
  72.              * 定义显示日期的公共格式     
  73.              * 如:yyyy-MM-dd hh:mm     
  74.              * */       
  75.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        
  76.             String newdate = sdf.format(new Date());        
  77.             // 填充出产日期        
  78.             label = new Label(4,1,newdate);        
  79.             sheet.addCell(label);        
  80.             // 填充产地        
  81.             label = new Label(5,1,"陕西西安");        
  82.             sheet.addCell(label);        
  83.             /*     
  84.              * 显示布尔值     
  85.              * */       
  86.             jxl.write.Boolean bool = new jxl.write.Boolean(6,1,true);        
  87.             sheet.addCell(bool);        
  88.             /*     
  89.              * 合并单元格     
  90.              * 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的     
  91.              * 表示将从第x+1列,y+1行到m+1列,n+1行合并     
  92.              *      
  93.              * */       
  94.             sheet.mergeCells(0,3,2,3);        
  95.             label = new Label(0,3,"合并了三个单元格");        
  96.             sheet.addCell(label);        
  97.             /*     
  98.              *      
  99.              * 定义公共字体格式     
  100.              * 通过获取一个字体的样式来作为模板     
  101.              * 首先通过web.getSheet(0)获得第一个sheet     
  102.              * 然后取得第一个sheet的第二列,第一行也就是"产品名称"的字体      
  103.              * */       
  104.             CellFormat cf = wwb.getSheet(0).getCell(10).getCellFormat();        
  105.             WritableCellFormat wc = new WritableCellFormat();        
  106.             // 设置居中        
  107.             wc.setAlignment(Alignment.CENTRE);        
  108.             // 设置边框线        
  109.             wc.setBorder(Border.ALL, BorderLineStyle.THIN);        
  110.             // 设置单元格的背景颜色        
  111.             wc.setBackground(jxl.format.Colour.RED);        
  112.             label = new Label(1,5,"字体",wc);        
  113.             sheet.addCell(label);        
  114.        
  115.             // 设置字体        
  116.             jxl.write.WritableFont wfont = new jxl.write.WritableFont(WritableFont.createFont("隶书"),20);        
  117.             WritableCellFormat font = new WritableCellFormat(wfont);        
  118.             label = new Label(2,6,"隶书",font);        
  119.             sheet.addCell(label);        
  120.                     
  121.             // 写入数据        
  122.             wwb.write();        
  123.             // 关闭文件        
  124.             wwb.close();        
  125.             long end = System.currentTimeMillis();        
  126.             System.out.println("----完成该操作共用的时间是:"+(end-start)/1000);        
  127.         } catch (Exception e) {        
  128.             System.out.println("---出现异常---");        
  129.             e.printStackTrace();        
  130.         }        
  131.      
  132.     }     
  133. }<SPAN style="FONT-FAMILY: Simsun; WHITE-SPACE: normal; FONT-SIZE: medium"><PRE class=java name="code">package excel;  </PRE> </SPAN>  
package cztest;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.CellFormat;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class JxlTest {

    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
         // 准备设置excel工作表的标题     
        String[] title = {"编号","产品名称","产品价格","产品数量","生产日期","产地","是否出口"};     
        try {     
            // 获得开始时间     
            long start = System.currentTimeMillis();     
            // 输出的excel的路径     
            String filePath = "c:\\test.xls";     
            // 创建Excel工作薄     
            WritableWorkbook wwb;     
            // 新建立一个jxl文件,即在C盘下生成test.xls     
            OutputStream os = new FileOutputStream(filePath);     
            wwb=Workbook.createWorkbook(os);      
            // 添加第一个工作表并设置第一个Sheet的名字     
            WritableSheet sheet = wwb.createSheet("产品清单", 0);     
            Label label;     
            for(int i=0;i<title.length;i++){     
                // Label(x,y,z)其中x代表单元格的第x+1列,第y+1行, 单元格的内容是y     
                // 在Label对象的子对象中指明单元格的位置和内容     
                label = new Label(i,0,title[i]);     
                // 将定义好的单元格添加到工作表中     
                sheet.addCell(label);     
            }     
            // 下面是填充数据     
            /*    
             * 保存数字到单元格,需要使用jxl.write.Number   
             * 必须使用其完整路径,否则会出现错误   
             * */    
            // 填充产品编号     
            jxl.write.Number number = new jxl.write.Number(0,1,20071001);     
            sheet.addCell(number);     
            // 填充产品名称     
            label = new Label(1,1,"金鸽瓜子");     
            sheet.addCell(label);     
            /*   
             * 定义对于显示金额的公共格式   
             * jxl会自动实现四舍五入   
             * 例如 2.456会被格式化为2.46,2.454会被格式化为2.45   
             * */    
            jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");     
            jxl.write.WritableCellFormat wcf = new jxl.write.WritableCellFormat(nf);     
            // 填充产品价格     
            jxl.write.Number nb = new jxl.write.Number(2,1,2.45,wcf);     
            sheet.addCell(nb);     
            // 填充产品数量     
            jxl.write.Number numb = new jxl.write.Number(3,1,200);     
            sheet.addCell(numb);     
            /*   
             * 定义显示日期的公共格式   
             * 如:yyyy-MM-dd hh:mm   
             * */    
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     
            String newdate = sdf.format(new Date());     
            // 填充出产日期     
            label = new Label(4,1,newdate);     
            sheet.addCell(label);     
            // 填充产地     
            label = new Label(5,1,"陕西西安");     
            sheet.addCell(label);     
            /*   
             * 显示布尔值   
             * */    
            jxl.write.Boolean bool = new jxl.write.Boolean(6,1,true);     
            sheet.addCell(bool);     
            /*   
             * 合并单元格   
             * 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的   
             * 表示将从第x+1列,y+1行到m+1列,n+1行合并   
             *    
             * */    
            sheet.mergeCells(0,3,2,3);     
            label = new Label(0,3,"合并了三个单元格");     
            sheet.addCell(label);     
            /*   
             *    
             * 定义公共字体格式   
             * 通过获取一个字体的样式来作为模板   
             * 首先通过web.getSheet(0)获得第一个sheet   
             * 然后取得第一个sheet的第二列,第一行也就是"产品名称"的字体    
             * */    
            CellFormat cf = wwb.getSheet(0).getCell(1, 0).getCellFormat();     
            WritableCellFormat wc = new WritableCellFormat();     
            // 设置居中     
            wc.setAlignment(Alignment.CENTRE);     
            // 设置边框线     
            wc.setBorder(Border.ALL, BorderLineStyle.THIN);     
            // 设置单元格的背景颜色     
            wc.setBackground(jxl.format.Colour.RED);     
            label = new Label(1,5,"字体",wc);     
            sheet.addCell(label);     
    
            // 设置字体     
            jxl.write.WritableFont wfont = new jxl.write.WritableFont(WritableFont.createFont("隶书"),20);     
            WritableCellFormat font = new WritableCellFormat(wfont);     
            label = new Label(2,6,"隶书",font);     
            sheet.addCell(label);     
                 
            // 写入数据     
            wwb.write();     
            // 关闭文件     
            wwb.close();     
            long end = System.currentTimeMillis();     
            System.out.println("----完成该操作共用的时间是:"+(end-start)/1000);     
        } catch (Exception e) {     
            System.out.println("---出现异常---");     
            e.printStackTrace();     
        }     
  
    }  
}
  
  
Java代码 复制代码
  1. package excel;    


 
 
Java代码 复制代码
  1.      
  2. import java.io.FileOutputStream;     
  3. import java.text.SimpleDateFormat;     
  4. import java.util.Date;     
  5.      
  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;     
  7. import org.apache.poi.ss.usermodel.Cell;     
  8. import org.apache.poi.ss.usermodel.CellStyle;     
  9. import org.apache.poi.ss.usermodel.DataFormat;     
  10. import org.apache.poi.ss.usermodel.Row;     
  11. import org.apache.poi.ss.usermodel.Sheet;     
  12. import org.apache.poi.ss.usermodel.Workbook;     
  13. import org.apache.poi.ss.util.CellRangeAddress;     
  14.      
  15.      
  16. /***********************************************************************      
  17.  *      
  18.  *   poiCreate.java        
  19.  *   @copyright       Copyright:   2009-2012        
  20.  *   @creator         周辉<br/>      
  21.  *   @create-time   Mar 9, 2010   2:27:52 PM      
  22.  *   @revision         $Id:     *      
  23.  ***********************************************************************/     
  24. public class poiCreate {     
  25.      
  26.     /**   
  27.      * @param args   
  28.      */     
  29.     public static void main(String[] args) throws Exception {     
  30.         //创建一个EXCEL     
  31.         Workbook wb = new HSSFWorkbook();     
  32.         DataFormat format = wb.createDataFormat();     
  33.         CellStyle style;     
  34.         //创建一个SHEET     
  35.         Sheet sheet1 = wb.createSheet("产品清单");     
  36.         String[] title = {"编号","产品名称","产品价格","产品数量","生产日期","产地","是否出口"};     
  37.         int i=0;     
  38.         //创建一行     
  39.         Row row = sheet1.createRow((short)0);     
  40.         //填充标题     
  41.         for (String  s:title){     
  42.             Cell cell = row.createCell(i);     
  43.             cell.setCellValue(s);     
  44.             i++;     
  45.         }     
  46.         Row row1 = sheet1.createRow((short)1);     
  47.         //下面是填充数据     
  48.         row1.createCell(0).setCellValue(20071001);     
  49.         row1.createCell(1).setCellValue("金鸽瓜子");     
  50.         //创建一个单元格子     
  51.         Cell cell2=row1.createCell(2);     
  52.         // 填充产品价格     
  53.         cell2.setCellValue(2.45);     
  54.         style = wb.createCellStyle();     
  55.         style.setDataFormat(format.getFormat("#.##"));     
  56.         //设定样式     
  57.         cell2.setCellStyle(style);     
  58.         // 填充产品数量     
  59.         row1.createCell(3).setCellValue(200);     
  60.         /*     
  61.          * 定义显示日期的公共格式     
  62.          * 如:yyyy-MM-dd hh:mm     
  63.          * */     
  64.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        
  65.         String newdate = sdf.format(new Date());      
  66.         // 填充出产日期        
  67.         row1.createCell(4).setCellValue(newdate);     
  68.         row1.createCell(5).setCellValue("陕西西安");     
  69.         /*     
  70.          * 显示布尔值     
  71.          * */      
  72.         row1.createCell(6).setCellValue(true);     
  73.         /*     
  74.          * 合并单元格     
  75.          * 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的     
  76.          * 表示将first row, last row,first column,last column   
  77.          *      
  78.          * */       
  79.         Row row2 = sheet1.createRow((short2);     
  80.          Cell cell3 = row2.createCell((short0);     
  81.          cell3.setCellValue("合并了三个单元格");     
  82.         sheet1.addMergedRegion(new CellRangeAddress(2,2,0,2));     
  83.              
  84.         FileOutputStream fileOut = new FileOutputStream("d:\\test.xls");     
  85.         wb.write(fileOut);     
  86.         fileOut.close();     
  87.      
  88.      
  89.     }     
  90.      
  91. }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值