導航:首頁 > 編程語言 > java讀取xlsx

java讀取xlsx

發布時間:2023-06-03 20:20:38

1. java POI讀取Excel的時候怎麼按列讀取

按列讀取的方法:
String pathname = "E:\\files\\title.xlsx";
File file = new File(pathname);
InputStream in = new FileInputStream(file);
//得到整個excel對象
XSSFWorkbook excel = new XSSFWorkbook(in);
//獲取整個excel有多少個sheet
int sheets = excel.getNumberOfSheets();
//便利第一個sheet
Map<String,String> colMap = new HashMap<String, String>();
for(int i = 0 ; i < sheets ; i++ ){
XSSFSheet sheet = excel.getSheetAt(i);
if(sheet == null){
continue;
}
int mergedRegions = sheet.getNumMergedRegions();
XSSFRow row2 = sheet.getRow(0);
Map<Integer,String> category = new HashMap<Integer, String>();
for(int j = 0 ; j < mergedRegions; j++ ){
CellRangeAddress rangeAddress = sheet.getMergedRegion(j);
int firstRow = rangeAddress.getFirstColumn();
int lastRow = rangeAddress.getLastColumn();
category.put(rangeAddress.getFirstColumn(), rangeAddress.getLastColumn()+"-"+row2.getCell(firstRow).toString());
}
//便利每一行
for( int rowNum = 1 ; rowNum <= sheet.getLastRowNum() ; rowNum++ ){
System.out.println();
XSSFRow row = sheet.getRow(rowNum);
if(row == null){
continue;
}
short lastCellNum = row.getLastCellNum();
String cate = "";
Integer maxIndex = 0;
for( int col = row.getFirstCellNum() ; col < lastCellNum ; col++ ){
XSSFCell cell = row.getCell(col);
if(cell == null ){
continue;
}
if("".equals(cell.toString())){
continue;
}
int columnIndex = cell.getColumnIndex();
String string = category.get(columnIndex);
if(string != null && !string.equals("")){
String[] split = string.split("-");
cate = split[1];
maxIndex = Integer.parseInt(split[0]);
System.out.println(cate+"<-->"+cell.toString());
}else {
//如果當前便利的列編號小於等於合並單元格的結束,說明分類還是上面的分類名稱
if(columnIndex<=maxIndex){
System.out.println(cate+"<-->"+cell.toString());
}else {
System.out.println("分類未知"+"<-->"+cell.toString());
}
}
}
}
}
}

2. 如何使用java從excel表提取內容

以下是使用java從excel表提取內容的程序 -
import java.io.File;import java.io.FileInputStream;import org.apache.tika.metadata.Metadata;import org.apache.tika.parser.ParseContext;import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;import org.apache.tika.sax.BodyContentHandler;public class ExtractContentFromExcel {
public static void main(String args[]) throws Exception {

// detecting the file type
BodyContentHandler handler = new BodyContentHandler();

Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(new File("excelExample.xlsx"));

ParseContext pcontext = new ParseContext();

// OOXml parser
OOXMLParser msofficeparser = new OOXMLParser();

msofficeparser.parse(inputstream, handler, metadata, pcontext);
System.out.println("Contents of the document:" + handler.toString());
System.out.println("Metadata of the document:");
String[] metadataNames = metadata.names();

for (String name : metadataNames) {
System.out.println(name + ": " + metadata.get(name));
}
}}Java

原ODF文件:excelExample.xlsx 的內容如下 -

執行上面示例代碼,得到以下結果 -
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/F:/worksp/javaexamples/libs/tika_libs/tika-app-1.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/F:/worksp/javaexamples/libs/tika_libs/tika-server-1.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
九月 27, 2017 5:15:47 上午 org.apache.tika.config.InitializableProblemHandler$3 handleInitializableProblem
警告: JBIG2ImageReader not loaded. jbig2 files will be ignored
See http://pdfbox.apache.org/2.0/dependencies.html#jai-image-io
for optional dependencies.
TIFFImageWriter not loaded. tiff files will not be processed
See http://pdfbox.apache.org/2.0/dependencies.html#jai-image-io
for optional dependencies.
J2KImageReader not loaded. JPEG2000 files will not be processed.
See http://pdfbox.apache.org/2.0/dependencies.html#jai-image-io
for optional dependencies.

九月 27, 2017 5:15:47 上午 org.apache.tika.config.InitializableProblemHandler$3 handleInitializableProblem
警告: org.xerial's sqlite-jdbc is not loaded.
Please provide the jar on your classpath to parse sqlite files.
See tika-parsers/pom.xml for the correct version.
Contents of the document:Sheet1
編號 姓名 年齡 工作
1001 王傳大 22 Java軟體開發
1002 李小雙 29 項目經理
1020 張在傳 28 人事經理

Sheet2

Sheet3

Metadata of the document:
date: 2017-09-27T09:15:38Z
meta:creation-date: 2017-09-27T09:13:50Z
extended-properties:Application: Microsoft Excel
Creation-Date: 2017-09-27T09:13:50Z
dcterms:created: 2017-09-27T09:13:50Z
Last-Modified: 2017-09-27T09:15:38Z
dcterms:modified: 2017-09-27T09:15:38Z
Last-Save-Date: 2017-09-27T09:15:38Z
protected: false
meta:save-date: 2017-09-27T09:15:38Z
Application-Name: Microsoft Excel
modified: 2017-09-27T09:15:38Z
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
custom:KSOProctBuildVer: 2052-10.1.0.6489Shell

易百教程移動端:請掃描本頁面底部(右側)二維碼並關注微信公眾號,回復:"教程" 選擇相關教程閱讀或直接訪問:http://m.yii.com 。

3. java怎麼獲取excel中的數據

使用java poi
package webservice;

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;

public class ExcelController {

@SuppressWarnings("deprecation")
public void excel() throws FileNotFoundException, IOException{

String filename = "d:\\excel.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
//按名引用excel工作表
// HSSFSheet sheet = workbook.getSheet("JSP");
//用式獲取excel工作表採用工作表索引值
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row ;
HSSFCell cell1;
int rows=sheet.getLastRowNum();
for(int icount=0;icount<rows;icount++){
row = sheet.getRow(icount);
int line=row.getPhysicalNumberOfCells();
for(int j=0;j<line;j++){
cell1= row.getCell(j);
System.out.println(cell1+"--"+icount+"---"+j);
}
}
//列印讀取值
// System.out.println(cell.getStringCellValue());

//新建輸流
FileOutputStream fout = new FileOutputStream(filename); //PS:filename 另存路徑處理直接寫入模版文件
//存檔
workbook.write(fout);
fout.flush();
//結束關閉
fout.close();

}

public HSSFCell getCell(HSSFRow row, int index) {

// 取發期單元格
HSSFCell cell = row.getCell(index);

// 單元格存
if (cell == null) {

// 創建單元格
cell = row.createCell(index);
}

// 返單元格
return cell;
}

public static void main(String[] args) {
ExcelController ec = new ExcelController();
try {
ec.excel();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

4. 用java讀excel的數據。

通過java的poi讀取xlsx文件:

將excel文件使用文件流讀取

把excel的文件流轉化成excel的工作薄

獲取工作薄的sheet(工作表),迭代獲取

最後再次迭代每一個sheet,獲取每一行的每一個單元格,把數據取出存儲起來

5. 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);
}
}

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

在java程序添加spire.xls.jar依賴

importcom.spire.xls.*;

publicclassReadExcel{
publicstaticvoidmain(String[]args){

//創建Workbook對象
Workbookwb=newWorkbook();
//載入一個Excel文檔
wb.loadFromFile("C:\Users\Administrator\Desktop\test.xlsx");
//獲取第一個工作表
Worksheetsheet=wb.getWorksheets().get(0);
//遍歷工作表的每一行
for(inti=1;i<sheet.getLastRow()+1;i++){
//遍歷工作的每一列
for(intj=1;j<sheet.getLastColumn()+1;j++){
//輸出指定單元格的數據
System.out.print(sheet.get(i,j).getText());
System.out.print(" ");
}
System.out.print(" ");
}
}
}

閱讀全文

與java讀取xlsx相關的資料

熱點內容
青春期2裡面的跳舞的歌 瀏覽:35
國產動作愛情片 瀏覽:418
韓國有部特種兵與護士的電影 瀏覽:660
《貪婪》中的日本女演員 瀏覽:476
男主得艾滋病的電影 瀏覽:806
罪孽船長泰國版在線觀看 瀏覽:193
外國電影一個黑男孩在深林 瀏覽:902
叔嫂不倫之戀電影 瀏覽:211
溫暖溫柔是哪部小說 瀏覽:203
穿越抗戰自立為軍閥的小說 瀏覽:601
韓國強殲電影有哪些 瀏覽:291
一女二三男小說穿越文 瀏覽:824
台灣用哪個軟體看電視 瀏覽:365
父親為救兒子像男人獻身的泰國電影 瀏覽:72
台灣民族片子 瀏覽:4
香港老公出軌電影 瀏覽:462
黑社會後生可畏國語 瀏覽:137
韓國肉肉電影在線觀看 瀏覽:345
中文版韓國倫理電影 瀏覽:397
皇上叫秦風女主是宮女的小說 瀏覽:912