chengzhi 发表于 2013-1-15 01:54:51

jxl poi 操作excl

 
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);                     // 将定义好的单元格添加到工作表中                     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();             }         }}package excel;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.Date;    import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.DataFormat;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;      /***********************************************************************   *   *   poiCreate.java       *   @copyright       Copyright:   2009-2012       *   @creator         周辉<br/>   *   @create-time   Mar 9, 2010   2:27:52 PM   *   @revision         $Id:   *   ***********************************************************************/public class poiCreate {      /**      * @param args      */      public static void main(String[] args) throws Exception {          //创建一个EXCEL          Workbook wb = new HSSFWorkbook();          DataFormat format = wb.createDataFormat();          CellStyle style;          //创建一个SHEET          Sheet sheet1 = wb.createSheet("产品清单");          String[] title = {"编号","产品名称","产品价格","产品数量","生产日期","产地","是否出口"};          int i=0;          //创建一行          Row row = sheet1.createRow((short)0);          //填充标题          for (Strings:title){            Cell cell = row.createCell(i);            cell.setCellValue(s);            i++;          }          Row row1 = sheet1.createRow((short)1);          //下面是填充数据          row1.createCell(0).setCellValue(20071001);          row1.createCell(1).setCellValue("金鸽瓜子");          //创建一个单元格子          Cell cell2=row1.createCell(2);          // 填充产品价格          cell2.setCellValue(2.45);          style = wb.createCellStyle();          style.setDataFormat(format.getFormat("#.##"));          //设定样式          cell2.setCellStyle(style);          // 填充产品数量          row1.createCell(3).setCellValue(200);          /*            * 定义显示日期的公共格式            * 如:yyyy-MM-dd hh:mm            * */          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");             String newdate = sdf.format(new Date());         // 填充出产日期             row1.createCell(4).setCellValue(newdate);          row1.createCell(5).setCellValue("陕西西安");          /*            * 显示布尔值            * */         row1.createCell(6).setCellValue(true);          /*            * 合并单元格            * 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的            * 表示将first row, last row,first column,last column          *             * */            Row row2 = sheet1.createRow((short) 2);         Cell cell3 = row2.createCell((short) 0);         cell3.setCellValue("合并了三个单元格");          sheet1.addMergedRegion(new CellRangeAddress(2,2,0,2));                  FileOutputStream fileOut = new FileOutputStream("d:\\test.xls");          wb.write(fileOut);          fileOut.close();          }    } POI 用的JAR poi-3.6-20091214.jar   jxl 用到的jar  jxl-2.6.jar
页: [1]
查看完整版本: jxl poi 操作excl