java读取excel2010文件_java如何读写excel2010
展開全部
package com.b2bjy.crm.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIExcelUtil
{
/** *//** 總行數 */
private int totalRows = 0;
/** *//** 總列數 */
private int totalCells = 0;
/** *//** 構造方法 */
public POIExcelUtil()
{}
/** *//**
*
*
Description:[根據文件名讀取excel文件]*
Created by [Huyvanpull] [Jan 20, 2010]*
Midified by [modifier] [modified time]*
*
* @param fileName
* @return
* @throws Exception
*/
public ArrayList read(String fileName)
{
ArrayList dataLst = new ArrayList();
/** *//** 檢查文件名是否為空e68a84e8a2ad3231313335323631343130323136353331333335336536或者是否是Excel格式的文件 */
if (fileName == null || !fileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))
{
return dataLst;
}
boolean isExcel2003 = true;
/** *//** 對文件的合法性進行驗證 */
if (fileName.matches("^.+\\.(?i)(xlsx)$"))
{
isExcel2003 = false;
}
/** *//** 檢查文件是否存在 */
File file = new File(fileName);
if (file == null || !file.exists())
{
return dataLst;
}
try
{
/** *//** 調用本類提供的根據流讀取的方法 */
dataLst = read(new FileInputStream(file), isExcel2003);
}
catch (Exception ex)
{
ex.printStackTrace();
}
/** *//** 返回最后讀取的結果 */
return dataLst;
}
/** *//**
*
*
Description:[根據流讀取Excel文件]*
Created by [Huyvanpull] [Jan 20, 2010]*
Midified by [modifier] [modified time]*
*
* @param inputStream
* @param isExcel2003
* @return
*/
public ArrayList read(InputStream inputStream,
boolean isExcel2003)
{
ArrayList dataLst = null;
try
{
/** *//** 根據版本選擇創建Workbook的方式 */
Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream)
: new XSSFWorkbook(inputStream);
dataLst = read(wb);
}
catch (IOException e)
{
e.printStackTrace();
}
return dataLst;
}
/** *//**
*
*
Description:[得到總行數]*
Created by [Huyvanpull] [Jan 20, 2010]*
Midified by [modifier] [modified time]*
*
* @return
*/
public int getTotalRows()
{
return totalRows;
}
/** *//**
*
*
Description:[得到總列數]*
Created by [Huyvanpull] [Jan 20, 2010]*
Midified by [modifier] [modified time]*
*
* @return
*/
public int getTotalCells()
{
return totalCells;
}
/** *//**
*
*
Description:[讀取數據]*
Created by [Huyvanpull] [Jan 20, 2010]*
Midified by [modifier] [modified time]*
*
* @param wb
* @return
*/
private ArrayList read(Workbook wb)
{
ArrayList rowLst = new ArrayList();
/** *//** 得到第一個shell */
Sheet sheet = wb.getSheetAt(0);
this.totalRows = sheet.getPhysicalNumberOfRows();
if (this.totalRows >= 1 && sheet.getRow(0) != null)
{
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
/** *//** 循環Excel的行 */
for (int r = 0; r < this.totalRows; r++)
{
Row row = sheet.getRow(r);
if (row == null)
{
continue;
}
/** *//** 循環Excel的列 */
for (short c = 0; c < 1; c++)
{
Cell cell = row.getCell(c);
String cellValue = "";
if (cell == null)
{
rowLst.add(cellValue);
continue;
}
/** *//** 處理數字型的,自動去零 */
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType())
{
cellValue = getRightStr(cell.getNumericCellValue() + "");
}
/** *//** 處理字符串型 */
else if (Cell.CELL_TYPE_STRING == cell.getCellType())
{
cellValue = cell.getStringCellValue();
}
/** *//** 處理布爾型 */
else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType())
{
cellValue = cell.getBooleanCellValue() + "";
}
/** *//** 其它的,非以上幾種數據類型 */
else
{
cellValue = cell.toString() + "";
}
rowLst.add(cellValue);
}
}
return rowLst;
}
/** *//**
*
*
Description:[正確地處理整數后自動加零的情況]*
Created by [Huyvanpull] [Jan 20, 2010]*
Midified by [modifier] [modified time]*
*
* @param sNum
* @return
*/
private String getRightStr(String sNum)
{
DecimalFormat decimalFormat = new DecimalFormat("#.000000");
String resultStr = decimalFormat.format(new Double(sNum));
if (resultStr.matches("^[-+]?\\d+\\.[0]+$"))
{
resultStr = resultStr.substring(0, resultStr.indexOf("."));
}
return resultStr;
}
/** *//**
*
*
Description:[測試main方法]*
Created by [Huyvanpull] [Jan 20, 2010]*
Midified by [modifier] [modified time]*
*
* @param args
* @throws Exception
*/
public String getPhones(){
ArrayList dataLst = new POIExcelUtil().read("D:\\b.xls");
StringBuffer rowData = new StringBuffer();
for (int i = 0; i < dataLst.size()-1; i++) {
rowData.append(dataLst.get(i)).append(",");
}
rowData.append(dataLst.get(dataLst.size()-1));
if (rowData.length() > 0)
{
System.out.println(rowData);
}
return rowData.toString();
}
public static void main(String[] args) throws Exception
{
System.out.println(new POIExcelUtil().getPhones());
}
}
本回答由電腦網絡分類達人 董輝推薦
已贊過
已踩過<
你對這個回答的評價是?
評論
收起
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java读取excel2010文件_java如何读写excel2010的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java写大文件_java实现超大文件的
- 下一篇: 键盘按下某键 停止运行java_实现按下