導航:首頁 > 編程語言 > java讀取excel中數據

java讀取excel中數據

發布時間:2024-12-28 01:43:34

1. java如何讀取整個excel文件的內容

本例使用java來讀取excel的內容並展出出結果,代碼如下:

復制代碼 代碼如下:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcelOperate {

public static void main(String[] args) throws Exception {
File file = new File("ExcelDemo.xls");
String[][] result = getData(file, 1);
int rowLength = result.length;
for(int i=0;i<rowLength;i++) {
for(int j=0;j<result[i].length;j++) {
System.out.print(result[i][j]+"\t\t");
}
System.out.println();
}

}
/**
* 讀取Excel的內容,第一維數組存儲的是一行中格列的值,二維數組存儲的是多少個行
* @param file 讀取數據的源Excel
* @param ignoreRows 讀取數據忽略的行數,比喻行頭不需要讀入 忽略的行數為1
* @return 讀出的Excel中數據的內容
* @throws FileNotFoundException
* @throws IOException
*/
public static String[][] getData(File file, int ignoreRows)
throws FileNotFoundException, IOException {
List<String[]> result = new ArrayList<String[]>();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));
// 打開HSSFWorkbook
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
HSSFSheet st = wb.getSheetAt(sheetIndex);
// 第一行為標題,不取
for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = false;
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) {
// 注意:一定要設成這個,否則可能會出現亂碼
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 導入時如果為公式生成的數據則無值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
value = "";
}
}
if (columnIndex == 0 && value.trim().equals("")) {
break;
}
values[columnIndex] = rightTrim(value);
hasValue = true;
}

if (hasValue) {
result.add(values);
}
}
}
in.close();
String[][] returnArray = new String[result.size()][rowSize];
for (int i = 0; i < returnArray.length; i++) {
returnArray[i] = (String[]) result.get(i);
}
return returnArray;
}

/**
* 去掉字元串右邊的空格
* @param str 要處理的字元串
* @return 處理後的字元串
*/
public static String rightTrim(String str) {
if (str == null) {
return "";
}
int length = str.length();
for (int i = length - 1; i >= 0; i--) {
if (str.charAt(i) != 0x20) {
break;
}
length--;
}
return str.substring(0, length);
}
}

2. 怎麼獲取excel字元間列java

一、excel讀取的兩種方式
Java中解析excel的方式,我目前知道的有兩種,一種是 jxl 讀取,另一種是 poi 讀取

1.1 jxl 和 poi 的區缺旦虧別和選擇
① jxl 只能解析 xls 文件,不能 解析 xlsx 文件; poi 則是可伏神以同時兼容xls 和xlsx兩種文件類型,這是要注意的第一個點;

② 這遲改兩個方法的讀取方式不一樣,jxl 讀取的是 先讀列 然後循環獲取的該列每行的信息。poi 讀取是 先讀行,再循環獲取每列的信息。如下圖:

閱讀全文

與java讀取excel中數據相關的資料

熱點內容
工行app里哪裡看我的網銀 瀏覽:9
phplinux一鍵安裝包 瀏覽:193
軟體租游戲用什麼伺服器 瀏覽:340
螺桿機壓縮機維修 瀏覽:8
監控系統設計原理是潮流演算法嗎 瀏覽:234
正品加密軟體來電咨詢 瀏覽:754
什麼叫數字幣APP 瀏覽:120
phppeclmac 瀏覽:11
前期副圖選股源碼 瀏覽:288
招聘程序員5年後感覺很萌新 瀏覽:612
光輝源碼 瀏覽:514
用大米解壓球 瀏覽:447
搭建音樂網站需要什麼伺服器 瀏覽:730
最新代掛網模板源碼 瀏覽:583
數據結構演算法與課程設計報告 瀏覽:464
釘釘程序員起飛視頻大全 瀏覽:554
土豆視頻推薦演算法 瀏覽:188
為什麼原生安卓bug少 瀏覽:563
python未知編碼怎麼解碼 瀏覽:969
at命令的作用 瀏覽:136