亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

java語言

Java中操作Excel表格方法

時(shí)間:2024-09-06 07:14:37 java語言 我要投稿
  • 相關(guān)推薦

Java中操作Excel表格方法

  引導(dǎo)語:我們都知道Excel 可以進(jìn)行各種數(shù)據(jù)的處理、統(tǒng)計(jì)分析和輔助決策操作,那么在Java中又是如何操作Excel 表格的呢,以下是百分網(wǎng)小編分享給大家的Java中操作Excel表格方法,歡迎閱讀!

  利用Java Excel API ,下載地址:jexcelapi.rar

  下面給出一段讀取數(shù)據(jù)的例子代碼:

  /*

  * To change this template, choose Tools | Templates

  * and open the template in the editor.

  */

  package excel;

  import java.io.FileInputStream;

  import java.io.FileNotFoundException;

  import java.io.IOException;

  import java.io.InputStream;

  import java.util.logging.Level;

  import java.util.logging.Logger;

  import jxl.Cell;

  import jxl.Sheet;

  import jxl.Workbook;

  import jxl.read.biff.BiffException;

  /**

  *

  * @author Wei.Liu

  */

  public class Main {

  /**

  * @param args the command line arguments

  */

  public static void main(String[] args) {

  try {

  InputStream is = new FileInputStream("d:\\test.xls");

  jxl.Workbook rwb = Workbook.getWorkbook(is);

  Sheet rs = rwb.getSheet(0);

  //取第一行,第一列的元素

  Cell c00 = rs.getCell(0, 0);

  String strc00 = c00.getContents();

  //取第一行,第二列的元素

  Cell c10 = rs.getCell(1,0);

  String strc10= c10.getContents();

  System.out.println(strc00+" "+c00.getType().toString());

  System.out.println(strc10+" "+c10.getType().toString());

  //獲取sheet的個(gè)數(shù)

  System.out.println(rwb.getNumberOfSheets());

  Sheet [] sheets =rwb.getSheets();

  for(int i=0;i

  System.out.println(rwb.getSheet(i).getName());

  }

  //獲得列數(shù)

  System.out.println(rwb.getSheet(0).getColumns());

  //獲得每列的元素

  Cell [] cell = rwb.getSheet(0).getColumn(0);

  //獲得總行數(shù)

  System.out.println(rwb.getSheet(0).getRows());

  //獲得行元素

  Cell [] cell2 = rwb.getSheet(0).getRow(0);

  //關(guān)閉對(duì)象

  rwb.close();

  } catch (Exception ex) {

  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

  }

  }

  }

  再給出一段,創(chuàng)建Excel表格的代碼:

  /*

  * To change this template, choose Tools | Templates

  * and open the template in the editor.

  */

  package excel;

  import java.io.File;

  import java.util.logging.Level;

  import java.util.logging.Logger;

  import jxl.Workbook;

  import jxl.write.Label;

  import jxl.write.WritableCellFormat;

  import jxl.write.WritableFont;

  import jxl.write.WritableSheet;

  import jxl.write.WritableWorkbook;

  import jxl.write.Number;

  /**

  *

  * @author Wei.Liu

  */

  public class Main {

  /**

  * @param args the command line arguments

  */

  public static void main(String[] args) {

  try {

  //創(chuàng)建EXCEL表格

  WritableWorkbook wwb = Workbook.createWorkbook(new File("d:\\test.xls"));

  //創(chuàng)建工作表

  WritableSheet ws = wwb.createSheet("Liu.Wei",0);

  //創(chuàng)建單元格

  ws.addCell(new Label(0,0,"Hello World"));

  //添加設(shè)置字體格式的單元格

  WritableFont wfc = new WritableFont(WritableFont.ARIAL,15,WritableFont.BOLD,true);

  WritableCellFormat wcff= new WritableCellFormat(wfc);

  Label labelcf = new Label(1,0,"Format text",wcff);

  ws.addCell(labelcf);

  //向單元格中寫入數(shù)字

  Number labelN = new Number(0,1,12345);

  ws.addCell(labelN);

  wwb.write();

  wwb.close();

  } catch (Exception ex) {

  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

  }

  }

  }

  最后給出一段更新表格的代碼:

  /*

  * To change this template, choose Tools | Templates

  * and open the template in the editor.

  */

  package excel;

  import java.io.File;

  import java.io.IOException;

  import java.util.logging.Level;

  import java.util.logging.Logger;

  import jxl.CellType;

  import jxl.Workbook;

  import jxl.read.biff.BiffException;

  import jxl.write.Label;

  import jxl.write.WritableCell;

  import jxl.write.WritableSheet;

  import jxl.write.WritableWorkbook;

  /**

  *

  * @author Wei.Liu

  */

  public class Main {

  /**

  * @param args the command line arguments

  */

  public static void main(String[] args) {

  try {

  Workbook rw = Workbook.getWorkbook(new File("d:\\test.xls"));

  WritableWorkbook wwb = Workbook.createWorkbook(new File("d:\\test.xls"),rw);

  //修改單元格的值

  WritableSheet ws = wwb.getSheet(0);

  WritableCell wc = ws.getWritableCell(0,0);

  if(wc.getType() == CellType.LABEL){

  Label l= (Label)wc;

  l.setString("Modified!!!");

  }

  wwb.write();

  wwb.close();

  rw.close();

  } catch (Exception ex) {

  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

  }

  }

  }

【Java中操作Excel表格方法】相關(guān)文章:

excel表格的基本操作方法之函數(shù)應(yīng)用10-28

簡單易學(xué)的Excel表格基本操作06-02

Excel表格中的公式之最06-21

Excel表格中如何換行10-25

Excel表格中怎么徹底隱藏?cái)?shù)據(jù)09-27

excel2007數(shù)據(jù)透視操作方法09-18

Java數(shù)組的基本操作方法介紹08-14

java日期時(shí)間基本操作方法08-08

excel中實(shí)現(xiàn)文本換行的方法06-01

excel表格的單元格輸入和公式編輯操作07-21