简单的excel读写

 

package com.wj;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ReadExcelNew {

 private List readExcel(String readPath ,int number)
 {
  List list = new ArrayList();
  Workbook rwb = null;
  Cell cell = null;
  //创建输入流
  InputStream stream = null;
  try {
   stream = new FileInputStream(readPath);
  } catch (FileNotFoundException e) {
   
   e.printStackTrace();
  }
  //获取Excel文件对象
  try {
   rwb = Workbook.getWorkbook(stream);
  } catch (BiffException e) {
   
   e.printStackTrace();
  } catch (IOException e) {
   
   e.printStackTrace();
  }
  //获取文件的指定工作表 默认的第一个
  Sheet sheet = rwb.getSheet(0);
        //循环列数
  for(int i=0;i   {
   String[] str = new String[sheet.getRows()];
   //循环行数
   for(int j=0;j    {
    cell = sheet.getCell(i, j);
    str[j] = cell.getContents();
   }
   list.add(str);
  }
  return list;
 }
 
 public void createExcel(String readPath,int number,String createPath)
 {
  List list = new ArrayList();
  ReadExcelNew tReadExcelNew = new ReadExcelNew();
  list = tReadExcelNew.readExcel(readPath, number);
  try
  {
   //打开文件
   WritableWorkbook book= Workbook.createWorkbook(new File(createPath));
   //生成名为"哈哈"的工作表,参数0表示这是第一页
   WritableSheet sheet1=book.createSheet("哈哈",0);
   
   //在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
   for(int i = 0;i    {
    String[] str = (String[])list.get(i);
    for (int j = 0; j < str.length; j++)
    {
     Label label=new Label(j,i,str[j]);
     //将定义好的单元格添加到工作表中   
     sheet1.addCell(label);
    }
   }

   //写入数据并关闭文件   
   book.write();
   book.close();
  }catch(Exception e)
  {
   System.out.println(e);
  }  
 }
 
 public static void main(String[] args) {
  
  ReadExcelNew tReadExcelNew = new ReadExcelNew();
  String readPath = "F:\\lisc\\WebRoot\\upload\\100999.xls";
  String createPath = "F:\\lisc\\WebRoot\\upload\\测试.xls";
  tReadExcelNew.createExcel(readPath, 1, createPath);
 }

}

运行前结果:

运行后结果:

你可能感兴趣的:(简单的excel读写)