java生成excel文件

java生成excel需要借助第三方的组件jxl.jar,这个组件网上有下载

package cn.com.testExcel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class TestExcelDemo {

 public void createExcelFile() {
  File f = new File("c:/aaa.xls");
  // 声明Excel文件
  WritableWorkbook workBook = null;
  try {
   workBook = Workbook.createWorkbook(f);
   writeExcelFile(workBook);
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    workBook.close();
   } catch (WriteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

 public void writeExcelFile(WritableWorkbook workBook) {
  // 创建sheet
  WritableSheet sheet = workBook.createSheet("测试", 0);
  try {
   //合并指定单元格()
   sheet.mergeCells(0, 0, 5, 0);
   
   //设置样式
   WritableCellFormat format = new WritableCellFormat();
   format.setAlignment(Alignment.CENTRE);

   Label labelTitle = new Label(0, 0, "标题", format);
   sheet.addCell(labelTitle);

   Label labelId = new Label(0, 1, "员工信息2", format);
   sheet.addCell(labelId);

   Label labelName = new Label(1, 1, "员工信息3", format);
   sheet.addCell(labelName);

   Label labelSal = new Label(2, 1, "员工信息4", format);
   sheet.addCell(labelSal);

   Vector allInfo = new Vector();
   Vector v1 = new Vector();
   v1.add(1);
   v1.add(1);
   v1.add(1);
   v1.add(1);
   Vector v2 = new Vector();
   v2.add(1);
   v2.add(1);
   v2.add(1);
   v2.add(1);
   Vector v3 = new Vector();
   v3.add(1);
   v3.add(1);
   v3.add(1);
   v3.add(1);

   allInfo.add(v1);
   allInfo.add(v2);
   allInfo.add(v3);

   Iterator iterator = allInfo.iterator();
   for (int rowNum = 2; iterator.hasNext(); rowNum++) {
    Vector a = (Vector) iterator.next();
    for (int i = 0; i < a.size(); i++) {
     sheet.addCell(new Label(i, rowNum, a.get(i).toString()));
    }
   }

  } catch (RowsExceededException e) {
   e.printStackTrace();
  } catch (WriteException e) {
   e.printStackTrace();
  }
  try {
   workBook.write();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 // //////////////////////////从Excel中读取数据/////////////////////////////////
 public Vector readExcel() {
  Vector allInfo = new Vector();
  File f = new File("c:/aaa.xls");
  FileInputStream in = null;
  try {
   in = new FileInputStream(f);

   Workbook workBook = Workbook.getWorkbook(in);
   Sheet sheet = workBook.getSheet(0);
   int row = sheet.getRows();

   for (int i = 2; i < row; i++) {
    System.out.println(sheet.getCell(0, i).getContents().toString()
      .trim());
    System.out.println(sheet.getCell(1, i).getContents().toString()
      .trim());
    System.out.println(sheet.getCell(2, i).getContents().toString()
      .trim());
    System.out.println(sheet.getCell(3, i).getContents().toString()
      .trim());
   }

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (BiffException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

  return allInfo;
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  TestExcelDemo t = new TestExcelDemo();
  t.createExcelFile();
  // t.readExcel();
 }

}

 

 

总结一下步骤:

往excel文件中添加数据

1,创建一个excel文件的操作流WritableWorkBook

2,创建一个工作簿WorkBook.Workbook对象代表一个工作簿,Workbooks集合对象则代表同一Excel进程中打开的所有工作簿对象。

3,创建一个表单:WritableSheet sheet = workBook.createSheet("商品", 0);

4,创建文件的格式:WritableSheet sheet = workBook.createSheet("商品", 0);

5,向文件中添加内容


从excel文件中读出数据

1,创建一个InputStream 流

2,创建一个工作簿WorkBook

3,获得要读取的表单    Sheet sheet = workBook.getSheet(0);

4,依次取得元素

你可能感兴趣的:(java,工作,Excel,单元测试,F#)