日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java聚合excel_java操作excel

發布時間:2024/10/12 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java聚合excel_java操作excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、工具準備

(1)下載poi.jar

(2)eclipse

2、Excel準備

3、代碼示例

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

//操作Excel工具類

public class ExcelUtils {

public static HSSFSheet ExcelSheet;

public static HSSFWorkbookExcelBook;

public static HSSFRow Row;

public static HSSFCellCell;

/**

* 加載Excel

* @param Path

* 文件路徑

*/

public static void setExcelFile(String Path) {

FileInputStream ExcelFile;

try {

ExcelFile = new FileInputStream(Path);

ExcelBook=new HSSFWorkbook(ExcelFile);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 值得寫入

* @param Result

* @param RowNum

* @param ColNum

* @param Path

* @param SheetName

*/

public static void setCellData(String Result, int RowNum, int ColNum,String Path,String SheetName) {

try {

ExcelSheet=ExcelBook.getSheet(SheetName);

Row = ExcelSheet.getRow(RowNum);

Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);

if (Cell == null) {

Cell = Row.createCell(ColNum);

Cell.setCellValue(Result);

} else {

Cell.setCellValue(Result);

}

FileOutputStream fileOut = new FileOutputStream(Path);

ExcelBook.write(fileOut);

fileOut.flush();

fileOut.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 獲取Excel中對應單元格的值

* @param RowNum

* @param CloNum

* @param SheetName

* @return

*/

public static String getCellDate(int RowNum,int CloNum,String SheetName){

ExcelSheet=ExcelBook.getSheet(SheetName);

Cell=ExcelSheet.getRow(RowNum).getCell(CloNum);

String cellData=Cell.getStringCellValue();

return cellData;

}

/**

* 獲取到sheet頁最后一行

* @param SheetName

* @return

*/

public static int getLastRowNums(String SheetName) {

try {

ExcelSheet=ExcelBook.getSheet(SheetName);

int rowCount = ExcelSheet.getLastRowNum();

return rowCount;

}catch (Exception e){

throw(e);

}

}

}

總結

以上是生活随笔為你收集整理的java聚合excel_java操作excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。