在Java中对Excel操作

创建一个文件temp,同时在temp 目录下创建一个outpath.xls

写操作

 

package com;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.NumberFormat;
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 Demo {
//定义一个静态变量
   private static String path="d:/temp/";
//查找目标文件是否存在
   static void   checkExist(String   path){      
       java.io.File   file   =   new   java.io.File(path);      
       if(file.isDirectory()){      
           System.out.println("the   dir   is   exits");
           file.delete();
       }else{      
           file.mkdirs();      
           System.out.println("made   a   dir   !"   );      
       }      
   }   

   public static void main(String args[]) throws IOException, RowsExceededException, WriteException{
    Demo.checkExist(path);
    File tempFile=new File(path+"outpath.xls");
    WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
    WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
//一些临时变量,用于写到excel中
    Label l=null;
    jxl.write.Number n=null;
    jxl.write.DateTime d=null;

//预定义的一些字体和格式,同一个Excel中最好不要有太多格式
    WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
    WritableCellFormat headerFormat = new WritableCellFormat (headerFont);

    WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
    WritableCellFormat titleFormat = new WritableCellFormat (titleFont);

    WritableFont detFont = new WritableFont(WritableFont.ARIAL,10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
    WritableCellFormat detFormat = new WritableCellFormat (detFont);

    NumberFormat nf=new NumberFormat("0.00000");  //用于Number的格式
    WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf);
   
    Label label1 = new Label(1, 1, "用于测试的Excel文件", titleFormat);
    Label label2 = new Label(1, 2, "用于测试的Excel文件", headerFormat);
    Label label3 = new Label(1, 3, "用于测试的Excel文件", detFormat);
    Label label4 = new Label(1, 4, "0.12450", priceFormat);
    sheet.addCell(label1);
    sheet.addCell(label2);
    sheet.addCell(label3);
    sheet.addCell(label4);
    workbook.write();
    workbook.close();
   }
}

读取操作

  FormFile formFile=(FormFile)dynaForm.get("objExcel");
  InputStream excelStream = formFile.getInputStream();
  Workbook workbook = Workbook.getWorkbook(excelStream);
  Sheet sheet = workbook.getSheet(0);
  int rows = sheet.getRows();
  for (int i = 1; i < rows; i++) {
   if (rows < 2) {
    this.writeJsMessage(response,
        "alert('请输入信息');window.parent.returnValue='ok';window.parent.close();");
   }
   String xsbh = sheet.getCell(0, i).getContents().trim();

 }

你可能感兴趣的:(java,String,File,测试,Excel,Path)