在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();

 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值