jexcel 导出excel文件的demo

jexcel是java导出excel的一个开源工具包。有的时候需要将数据导到excel文件中以便观看。

需要上网下载jxl.jar

核心代码如下:

package test;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.NumberFormats;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class TutorialMain {

 /**
  * WritingSpreadSheet
  */
 public void WritingSpreadSheet() {
  try {
   //创建一个WorkBook,即一个Excel文件,文件目录为param0
   WritableWorkbook workbook = Workbook.createWorkbook(new File("c:\\output.xls"));
   //创建一个Sheet,即表单,名字为param0;位置为param1
   WritableSheet sheet = workbook.createSheet("First Sheet Name", 0);

   sheet.setColumnView(0, 30); // 设置列的宽度
   sheet.setColumnView(1, 30); // 设置列的宽度
      sheet.setColumnView(2, 30); // 设置列的宽度
      sheet.setRowView(6, 1000); // 设置行的高度
      sheet.setRowView(4, 1000); // 设置行的高度
   sheet.setRowView(5, 1000); // 设置行的高度
   
   //创建一个Label,x坐标param0,y坐标param1,名字为param2
   Label label = new Label(0, 2, "A label record");
   //将该Label
   sheet.addCell(label);

   //创建一个Number,x坐标param0,y坐标param1,数值为param2
   Number number = new Number(3, 4, 3.1459);
   sheet.addCell(number);

   /*
    * 格式化信息
    */
   // Create a cell format for Arial 10 point font
   WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10);
   WritableCellFormat arial10format = new WritableCellFormat (arial10font);

   // Create the label, specifying content and format
   Label label2 = new Label(1,0, "Arial 10 point label", arial10format);
   sheet.addCell(label2);

   Label label3 = new Label(2, 0, "Another Arial 10 point label", arial10format);
   sheet.addCell(label3);

   // Create a cell format for Times 16, bold and italic
   WritableFont times16font = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, true);
   WritableCellFormat times16format = new WritableCellFormat (times16font);

   // Create the label, specifying content and format
   Label label4 = new Label(3,0, "Times 16 bold italic label", times16format);
   times16format.setBackground(jxl.format.Colour.BLUE);
   times16format.setAlignment(jxl.format.Alignment.CENTRE);
   sheet.addCell(label4);
   
   /*
    * 格式化数值
    */
   WritableCellFormat integerFormat = new WritableCellFormat (NumberFormats.INTEGER);
   Number number2 = new Number(0, 4, 3.141519, integerFormat);
   sheet.addCell(number2);

   WritableCellFormat floatFormat = new WritableCellFormat (NumberFormats.FLOAT);
   Number number3 = new Number(1, 4, 3.141519, floatFormat);
   sheet.addCell(number3);

   NumberFormat fivedps = new NumberFormat("#.#####");
   WritableCellFormat fivedpsFormat = new WritableCellFormat(fivedps);
   Number number4 = new Number(2, 4, 3.141519, fivedpsFormat);
   sheet.addCell(number4);

   WritableCellFormat fivedpsFontFormat = new WritableCellFormat (times16font, fivedps);
   Number number5 = new Number(3, 4, 3.141519, fivedpsFontFormat);
   sheet.addCell(number5);

   /*
    * Formatting Dates
    */
   // Get the current date and time from the Calendar object
   Date now = Calendar.getInstance().getTime();
   DateFormat customDateFormat = new DateFormat ("dd MM yyyy hh:mm:ss");
   WritableCellFormat dateFormat = new WritableCellFormat (customDateFormat);
   DateTime dateCell = new DateTime(0, 6, now, dateFormat);
   sheet.addCell(dateCell);

   /*
    * 合并单元格
    */
   WritableSheet sheet1 = workbook.createSheet("First Sheet", 1);
   sheet1.mergeCells(0, 0, 1, 1);
   
   // All sheets and cells added. Now write out the workbook
   workbook.write();
   workbook.close();
   
   System.out.println("创建成功..");
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("创建失败..");
   e.printStackTrace();
  } catch (RowsExceededException e) {
   // sheet.addCell Exception
   System.out.println("创建失败..");
   e.printStackTrace();
  } catch (WriteException e) {
   System.out.println("创建失败..");
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
 
 /**
  *
  * @param filePath 文件路径
  * @param fileName 文件名
  */
 public void ReadingSpreadSheets(String filePath,String fileName){
  try {
   Workbook workbook = Workbook.getWorkbook(new File(filePath + "\\" + fileName));
   Sheet sheet = workbook.getSheet(0);

   Cell a1 = sheet.getCell(0,0);
   Cell b2 = sheet.getCell(1,1);
   Cell c2 = sheet.getCell(2,1);

   String stringa1 = a1.getContents();
   String stringb2 = b2.getContents();
   String stringc2 = c2.getContents();

   double numberb2 = 0;
   Date datec2 = null;

   if (a1.getType() == CellType.LABEL)
   {
     LabelCell lc = (LabelCell) a1;
     stringa1 = lc.getString();
   }

   if (b2.getType() == CellType.NUMBER)
   {
     NumberCell nc = (NumberCell) b2;
     numberb2 = nc.getValue();
   }

   if (c2.getType() == CellType.DATE)
   {
     DateCell dc = (DateCell) c2;
     datec2 = dc.getDate();
   }

   // 其他处理
   // ...

   // 结束后关闭文件并释放内存
   workbook.close();
   System.out.println("读取成功..");
   
  } catch (BiffException e) {
   // TODO Auto-generated catch block
   System.out.println("读取失败..");
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("读取失败..");
   e.printStackTrace();
  }

 }
 
 public void copyWorkBook(String oldFilePath,String oldFileName,String newFilePath,String newFileName ){
  try {
   Workbook workbook = Workbook.getWorkbook(new File( oldFilePath + "\\" + oldFileName ));
   WritableWorkbook copy = Workbook.createWorkbook(new File( newFilePath +"\\" +newFileName ), workbook);

   //进行一些操作
   WritableSheet sheet = copy.getSheet(1);
   WritableCell cell0 = sheet.getWritableCell(1, 2);

   if (cell0.getType() == CellType.LABEL)
   {
     Label l0 = (Label) cell0;
     l0.setString("modified cell");
   }
   
   WritableSheet sheet2 = copy.getSheet(1);
   WritableCell cell = sheet2.getWritableCell(2, 4);

   NumberFormat fivedps = new NumberFormat("#.#####");
   WritableCellFormat cellFormat = new WritableCellFormat(fivedps);
   cell.setCellFormat(cellFormat);
   
   Label label = new Label(0, 2, "New label record");
   sheet2.addCell(label);

   Number number = new Number(3, 4, 3.1459);
   sheet2.addCell(number);
   
   // 所有的都完成,关闭资源
   workbook.close();
   copy.write();
   copy.close();

  } catch (BiffException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (RowsExceededException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (WriteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  
 }
 
 public static void main(String[] args){
  TutorialMain main = new TutorialMain();
  main.WritingSpreadSheet();
 }
 

}
package test;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.NumberFormats;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class TutorialMain {

 /**
  * WritingSpreadSheet
  */
 public void WritingSpreadSheet() {
  try {
   //创建一个WorkBook,即一个Excel文件,文件目录为param0
   WritableWorkbook workbook = Workbook.createWorkbook(new File("c:\\output.xls"));
   //创建一个Sheet,即表单,名字为param0;位置为param1
   WritableSheet sheet = workbook.createSheet("First Sheet Name", 0);

   sheet.setColumnView(0, 30); // 设置列的宽度
   sheet.setColumnView(1, 30); // 设置列的宽度
      sheet.setColumnView(2, 30); // 设置列的宽度
      sheet.setRowView(6, 1000); // 设置行的高度
      sheet.setRowView(4, 1000); // 设置行的高度
   sheet.setRowView(5, 1000); // 设置行的高度
   
   //创建一个Label,x坐标param0,y坐标param1,名字为param2
   Label label = new Label(0, 2, "A label record");
   //将该Label
   sheet.addCell(label);

   //创建一个Number,x坐标param0,y坐标param1,数值为param2
   Number number = new Number(3, 4, 3.1459);
   sheet.addCell(number);

   /*
    * 格式化信息
    */
   // Create a cell format for Arial 10 point font
   WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10);
   WritableCellFormat arial10format = new WritableCellFormat (arial10font);

   // Create the label, specifying content and format
   Label label2 = new Label(1,0, "Arial 10 point label", arial10format);
   sheet.addCell(label2);

   Label label3 = new Label(2, 0, "Another Arial 10 point label", arial10format);
   sheet.addCell(label3);

   // Create a cell format for Times 16, bold and italic
   WritableFont times16font = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, true);
   WritableCellFormat times16format = new WritableCellFormat (times16font);

   // Create the label, specifying content and format
   Label label4 = new Label(3,0, "Times 16 bold italic label", times16format);
   times16format.setBackground(jxl.format.Colour.BLUE);
   times16format.setAlignment(jxl.format.Alignment.CENTRE);
   sheet.addCell(label4);
   
   /*
    * 格式化数值
    */
   WritableCellFormat integerFormat = new WritableCellFormat (NumberFormats.INTEGER);
   Number number2 = new Number(0, 4, 3.141519, integerFormat);
   sheet.addCell(number2);

   WritableCellFormat floatFormat = new WritableCellFormat (NumberFormats.FLOAT);
   Number number3 = new Number(1, 4, 3.141519, floatFormat);
   sheet.addCell(number3);

   NumberFormat fivedps = new NumberFormat("#.#####");
   WritableCellFormat fivedpsFormat = new WritableCellFormat(fivedps);
   Number number4 = new Number(2, 4, 3.141519, fivedpsFormat);
   sheet.addCell(number4);

   WritableCellFormat fivedpsFontFormat = new WritableCellFormat (times16font, fivedps);
   Number number5 = new Number(3, 4, 3.141519, fivedpsFontFormat);
   sheet.addCell(number5);

   /*
    * Formatting Dates
    */
   // Get the current date and time from the Calendar object
   Date now = Calendar.getInstance().getTime();
   DateFormat customDateFormat = new DateFormat ("dd MM yyyy hh:mm:ss");
   WritableCellFormat dateFormat = new WritableCellFormat (customDateFormat);
   DateTime dateCell = new DateTime(0, 6, now, dateFormat);
   sheet.addCell(dateCell);

   /*
    * 合并单元格
    */
   WritableSheet sheet1 = workbook.createSheet("First Sheet", 1);
   sheet1.mergeCells(0, 0, 1, 1);
   
   // All sheets and cells added. Now write out the workbook
   workbook.write();
   workbook.close();
   
   System.out.println("创建成功..");
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("创建失败..");
   e.printStackTrace();
  } catch (RowsExceededException e) {
   // sheet.addCell Exception
   System.out.println("创建失败..");
   e.printStackTrace();
  } catch (WriteException e) {
   System.out.println("创建失败..");
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
 
 /**
  *
  * @param filePath 文件路径
  * @param fileName 文件名
  */
 public void ReadingSpreadSheets(String filePath,String fileName){
  try {
   Workbook workbook = Workbook.getWorkbook(new File(filePath + "\\" + fileName));
   Sheet sheet = workbook.getSheet(0);

   Cell a1 = sheet.getCell(0,0);
   Cell b2 = sheet.getCell(1,1);
   Cell c2 = sheet.getCell(2,1);

   String stringa1 = a1.getContents();
   String stringb2 = b2.getContents();
   String stringc2 = c2.getContents();

   double numberb2 = 0;
   Date datec2 = null;

   if (a1.getType() == CellType.LABEL)
   {
     LabelCell lc = (LabelCell) a1;
     stringa1 = lc.getString();
   }

   if (b2.getType() == CellType.NUMBER)
   {
     NumberCell nc = (NumberCell) b2;
     numberb2 = nc.getValue();
   }

   if (c2.getType() == CellType.DATE)
   {
     DateCell dc = (DateCell) c2;
     datec2 = dc.getDate();
   }

   // 其他处理
   // ...

   // 结束后关闭文件并释放内存
   workbook.close();
   System.out.println("读取成功..");
   
  } catch (BiffException e) {
   // TODO Auto-generated catch block
   System.out.println("读取失败..");
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("读取失败..");
   e.printStackTrace();
  }

 }
 
 public void copyWorkBook(String oldFilePath,String oldFileName,String newFilePath,String newFileName ){
  try {
   Workbook workbook = Workbook.getWorkbook(new File( oldFilePath + "\\" + oldFileName ));
   WritableWorkbook copy = Workbook.createWorkbook(new File( newFilePath +"\\" +newFileName ), workbook);

   //进行一些操作
   WritableSheet sheet = copy.getSheet(1);
   WritableCell cell0 = sheet.getWritableCell(1, 2);

   if (cell0.getType() == CellType.LABEL)
   {
     Label l0 = (Label) cell0;
     l0.setString("modified cell");
   }
   
   WritableSheet sheet2 = copy.getSheet(1);
   WritableCell cell = sheet2.getWritableCell(2, 4);

   NumberFormat fivedps = new NumberFormat("#.#####");
   WritableCellFormat cellFormat = new WritableCellFormat(fivedps);
   cell.setCellFormat(cellFormat);
   
   Label label = new Label(0, 2, "New label record");
   sheet2.addCell(label);

   Number number = new Number(3, 4, 3.1459);
   sheet2.addCell(number);
   
   // 所有的都完成,关闭资源
   workbook.close();
   copy.write();
   copy.close();

  } catch (BiffException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (RowsExceededException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (WriteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  
 }
 
 public static void main(String[] args){
  TutorialMain main = new TutorialMain();
  main.WritingSpreadSheet();
 }
 

}

 

 

************************************************************************************************

下载整个工程的路径:

http://download.csdn.net/detail/kenchow126/4323264

 

你可能感兴趣的:(Date,String,object,Excel,calendar,File)