java操作Excle

新建Excle

package restart;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;

public class NewXls {

 public static String outputFile = "c:/junman/ gongye.xls";

 public static void main(String argv[]) {
  try {
   HSSFWorkbook workbook = new HSSFWorkbook();
   HSSFSheet sheet = workbook.createSheet();
   HSSFRow row = sheet.createRow((short) 0);
   HSSFCell cell = row.createCell((short) 0);
   cell.setCellType(HSSFCell.CELL_TYPE_STRING);
   cell.setCellValue("yestoday");
   FileOutputStream fOut = new FileOutputStream(outputFile);
   workbook.write(fOut);
   fOut.flush();
   fOut.close();
   System.out.println("文件生成...");
  } catch (Exception e) {
   System.out.println("已運行 xlCreate() : " + e);
  }
 }
}

 

讀取Excle

package restart;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.FileInputStream;
import java.io.FileOutputStream;// code run against the jakarta-poi-1.5.0-FINAL-20020506.jar.
import java.io.IOException;

public class Cls_Read_Write {
 static public void main(String[] args) throws Exception {
  // FileOutputStream fos = new FileOutputStream("c://scylla//java.xls");
  // HSSFWorkbook wb = new HSSFWorkbook();
  // HSSFSheet s = wb.createSheet();
  // wb.setSheetName(0, "first sheet");
  // HSSFRow row = s.createRow(0);
  // HSSFCell cell = row.createCell((short) 0);
  // cell.setCellValue("Hello! This message is generated from POI.");
  // wb.write(fos);
  // fos.close();

  POIFSFileSystem fs = null;
  HSSFWorkbook wb = null;
  try {
   fs = new POIFSFileSystem(new FileInputStream("c://scylla//java.xls"));
   wb = new HSSFWorkbook(fs);
  } catch (IOException e) {
   e.printStackTrace();
  }
  if(wb==null)
  {
   System.out.println("wb is null!");
   return;
  }
  else
  for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) {
   if (null != wb.getSheetAt(numSheets)) {
    HSSFSheet sheet = wb.getSheetAt(numSheets);// 獲得一個sheet
    for (int rowNumOfSheet = 0; rowNumOfSheet <= sheet.getLastRowNum(); rowNumOfSheet++) {
     if (null != sheet.getRow(rowNumOfSheet)) {
      HSSFRow row = sheet.getRow(rowNumOfSheet);// 獲得一個行
      for (short cellNumOfRow = 0; cellNumOfRow < row.getLastCellNum(); cellNumOfRow++) {
       if (null != row.getCell(cellNumOfRow)) {
        HSSFCell cell = row.getCell(cellNumOfRow);// 獲得列值
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);   
        if(cell.getCellType()==1){
         String msg = cell.getStringCellValue();
         System.out.print(msg + " ");
        }
        else if(cell.getCellType()==0){
         double msg = cell.getNumericCellValue();
         System.out.print(msg + " ");
        }    
        if (cellNumOfRow == row.getLastCellNum()-1) {
         System.out.println("");
        }
       }
      }
     }
    }
   }
  }
  // for (int numSheets = 0; numSheets <
  // wb.getNumberOfSheets();numSheets++) {
  // if (null != wb.getSheetAt(numSheets)) {
  // HSSFSheet aSheet = wb.getSheetAt(numSheets);// 獲得一個sheet
  // for (int rowNumOfSheet = 0; rowNumOfSheet <=
  // aSheet.getLastRowNum();rowNumOfSheet++) {
  // if (null != aSheet.getRow(rowNumOfSheet)) {
  // HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 獲得一個行
  // for (short cellNumOfRow = 0; cellNumOfRow <= aRow .getLastCellNum();
  // cellNumOfRow++) {
  // if (null != aRow.getCell(cellNumOfRow)) {
  // HSSFCell aCell = aRow.getCell(cellNumOfRow);// 獲得列值
  // content.append(aCell.getStringCellValue());
  // }
  // }
  // }
  // }
  // }
  // }
 }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值