java打开文件excel|java如何读取整个excel文件的内容

java打开文件excel|java如何读取整个excel文件的内容的第1张示图

⑴ java-怎么才能用Poi打开已有的excel文件,然后追加一些单元格阿

String filename = "excel名字.xls";res.setHeader("Content-Type", "application/vnd.ms-excel");res.setHeader("ContentDisposition", "attachment;filename=" + new String(filename.getBytes("MS932"), "ISO-8859-1"));调用一个方法,输出要输出的值,hm是个mapoutExcel(hm, res.getOutputStream());别看下面的方法代码多,其实有用的就几行,主要就是控制row和cell,设置单元格样式,赋值。private void outExcel(Map hm, ServletOutputStream out) throws IOException, Exception { String excelPath = excel模板路径; excelPath = new String(excelPath.getBytes("iso-8859-1"), "utf-8"); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelPath)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet1 = wb.getSheetAt(0); wb.setSheetName(0, "SheetName"); HSSFPrintSetup ps1 = sheet1.getPrintSetup(); ps1.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); HSSFRow row = sheet1.getRow((short) (1)); HSSFCell cell = row.getCell((short) (1)); String dataNumber = (String) hm.get("dataNumber"); cell.setCellValue(new HSSFRichTextString(dataNumber)); String birthAddress = (String) hm.get("birthAddress"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(birthAddress)); String acceptDate = (String) hm.get("acceptDate"); row = sheet1.getRow((short) (2)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(acceptDate)); String usedistrictCode = (String) hm.get("usedistrictCode"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(usedistrictCode)); String allegationZipCode = (String) hm.get("allegationZipCode"); row = sheet1.getRow((short) (3)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegationZipCode)); String birthName = (String) hm.get("birthName"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(birthName)); String allegationAddress = (String) hm.get("allegationAddress"); row = sheet1.getRow((short) (4)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegationAddress)); String birthTellNumber = (String) hm.get("birthTellNumber"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(birthTellNumber)); String allegationName = (String) hm.get("allegationName"); row = sheet1.getRow((short) (5)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegationName)); // 敪惗尮娆庬僐乕僪 String instrialCode = (String) hm.get("instrialCode"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(instrialCode)); // 怽棫幰背榖斣崋 String allegationTellNumber = (String) hm.get("allegationTellNumber"); row = sheet1.getRow((short) (6)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegationTellNumber)); // 敪惗尮巤愝摍 String birthFacilitiesetc = (String) hm.get("birthFacilitiesetc"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(birthFacilitiesetc)); // 怽棫撪梕 String allegation = (String) hm.get("allegation"); row = sheet1.getRow((short) (7)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegation)); // 挷嵏巜摫撪梕 String guidance = (String) hm.get("guidance"); row = sheet1.getRow((short) (8)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(guidance)); // 应掕擭宁担1 String sokuDate1 = (String) hm.get("sokuDate1"); row = sheet1.getRow((short) (9)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(sokuDate1)); // 应掕擭宁担2 String sokuDate2 = (String) hm.get("sokuDate2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(sokuDate2)); // 应掕擭宁担3 String sokuDate3 = (String) hm.get("sokuDate3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(sokuDate3)); // 应掕擭宁担4 String sokuDate4 = (String) hm.get("sokuDate4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(sokuDate4)); // 应掕擭宁担5 String sokuDate5 = (String) hm.get("sokuDate5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(sokuDate5)); // 应掕擭宁担6 String sokuDate6 = (String) hm.get("sokuDate6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(sokuDate6)); // 应掕帪岗1 String sokuTime1 = (String) hm.get("sokuTime1"); row = sheet1.getRow((short) (10)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(sokuTime1)); // 应掕帪岗2 String sokuTime2 = (String) hm.get("sokuTime2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(sokuTime2)); // 应掕帪岗3 String sokuTime3 = (String) hm.get("sokuTime3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(sokuTime3)); // 应掕帪岗4 String sokuTime4 = (String) hm.get("sokuTime4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(sokuTime4)); // 应掕帪岗5 String sokuTime5 = (String) hm.get("sokuTime5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(sokuTime5)); // 应掕帪岗6 String sokuTime6 = (String) hm.get("sokuTime6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(sokuTime6)); // 帋桠斣崋1 String shiryoNumber1 = (String) hm.get("shiryoNumber1"); row = sheet1.getRow((short) (11)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(shiryoNumber1)); // 帋桠斣崋2 String shiryoNumber2 = (String) hm.get("shiryoNumber2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(shiryoNumber2)); // 帋桠斣崋3 String shiryoNumber3 = (String) hm.get("shiryoNumber3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(shiryoNumber3)); // 帋桠斣崋4 String shiryoNumber4 = (String) hm.get("shiryoNumber4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(shiryoNumber4)); // 帋桠斣崋5 String shiryoNumber5 = (String) hm.get("shiryoNumber5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(shiryoNumber5)); // 帋桠斣崋6 String shiryoNumber6 = (String) hm.get("shiryoNumber6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(shiryoNumber6)); // 婥壏1 String temp1 = (String) hm.get("temp1"); row = sheet1.getRow((short) (12)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(temp1)); // 婥壏2 String temp2 = (String) hm.get("temp2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(temp2)); // 婥壏3 String temp3 = (String) hm.get("temp3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(temp3)); // 婥壏4 String temp4 = (String) hm.get("temp4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(temp4)); // 婥壏5 String temp5 = (String) hm.get("temp5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(temp5)); // 婥壏6 String temp6 = (String) hm.get("temp6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(temp6)); // 幖搙1 String humid1 = (String) hm.get("humid1"); row = sheet1.getRow((short) (13)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(humid1)); // 幖搙2 String humid2 = (String) hm.get("humid2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(humid2)); // 幖搙3 String humid3 = (String) hm.get("humid3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(humid3)); // 幖搙4 String humid4 = (String) hm.get("humid4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(humid4)); // 幖搙5 String humid5 = (String) hm.get("humid5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(humid5)); // 幖搙6 String humid6 = (String) hm.get("humid6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(humid6)); // 晽岦1 String windCode1 = (String) hm.get("windCode1"); row = sheet1.getRow((short) (14)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(windCode1)); // 晽岦2 String windCode2 = (String) hm.get("windCode2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(windCode2)); String windCode3 = (String) hm.get("windCode3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(windCode3)); String windCode4 = (String) hm.get("windCode4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(windCode4)); String windCode5 = (String) hm.get("windCode5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(windCode5)); String windCode6 = (String) hm.get("windCode6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(windCode6)); String windSpeed1 = (String) hm.get("windSpeed1"); row = sheet1.getRow((short) (15)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(windSpeed1)); String windSpeed2 = (String) hm.get("windSpeed2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(windSpeed2)); String windSpeed3 = (String) hm.get("windSpeed3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(windSpeed3)); String windSpeed4 = (String) hm.get("windSpeed4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(windSpeed4)); String windSpeed5 = (String) hm.get("windSpeed5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(windSpeed5)); String windSpeed6 = (String) hm.get("windSpeed6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(windSpeed6)); String pick1 = (String) hm.get("pick1"); row = sheet1.getRow((short) (16)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(pick1)); String pick2 = (String) hm.get("pick2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(pick2)); String pick3 = (String) hm.get("pick3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(pick3)); String pick4 = (String) hm.get("pick4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(pick4)); String pick5 = (String) hm.get("pick5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(pick5)); String pick6 = (String) hm.get("pick6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(pick6)); String smell1 = (String) hm.get("smell1"); row = sheet1.getRow((short) (17)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(smell1)); String smell2 = (String) hm.get("smell2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(smell2)); String smell3 = (String) hm.get("smell3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(smell3)); String smell4 = (String) hm.get("smell4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(smell4)); String smell5 = (String) hm.get("smell5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(smell5)); String smell6 = (String) hm.get("smell6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(smell6)); wb.write(out); out.close();}

⑵ 怎么用java程序直接打开一个Excel文件

到网上找一个poi的包,加载之后可以操纵Office 2003之前的版本文件,不支持2007以后的版本,包里面有具体的例子,对excel操作是最全面的。

⑶ 在java中 打开本地固定目录下的excle,并可以编辑!

如果你想打开excel必须下载到本机才行的,怎么可能在服务器直接打开给你显示出来并且编辑呢。说不定服务器上都没装office呢。你以前是用jsp页面操作那个excel时候并不是直接操作源文件的,是通过java来传递数据的。这样啊,如果你要打开一个excel就应该写成 try{ Runtime.getRuntime().exec("D:/Program Files/Kingsoft/WPS Office Personal/office6/et.exe D:\\1.xls"); } catch(IOException ieo) {} 明白了没 这两个 第一个是excel的程序 第二个是参数 就是用excel格式打开第二个文件 如果你的excel程序没有在系统的默认路径下 你就要加绝对路径 比如我的D:\Program Files\Kingsoft\WPS Office Personal\office6\et.exe我这里装的是wps软件,你可以看下自己装的office

⑷ java如何读取整个excel文件的内容

在Java中读取Excel文件的内容

在这里,我使用的是一个叫Java Excel API的东西,类似的还有的POI,不过感觉那个

太复杂了点儿。而且jxl对中文的支持相当的好,至少我在用的过程中一点问题没出。

一、下载地址

http://www.andykhan.com/jexcelapi/

二、特性

可以读取Excel 95, 97, 2000文件

可以读或写Excel 97及其以后版本的的公式(不过我发现好像有bug)

生成Excel 97格式的电子表格

支持字体、数字和日期格式化

支持单元格的颜色和阴影

可以编辑现有的文件

三、读文件

//声明一下,记得后面要关闭哦。。

Workbook workbook = null;

try {

workbook = Workbook.getWorkbook(new File("d:\temp\TestRead.xls"));

} catch (Exception e) {

throw new Exception("file to import not found!");

}

Sheet sheet = workbook.getSheet(0);

Cell cell = null;

int columnCount=3;

int rowCount=sheet.getRows();

for (int i = 0; i<rowcount; p="" {

for (int j = 0; j<columncount; p="" {

//注意,这里的两个参数,第一个是表示列的,第二才表示行

cell=sheet.getCell(j, i);

//要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确

if(cell.getType()==CellType.NUMBER){

System.out.print(((NumberCell)cell).getValue());

}

else if(cell.getType()==CellType.DATE){

System.out.print(((DateCell)cell).getDate());

}

else{

System.out.print(cell.getContents());

}

//System.out.print(cell.getContents());

System.out.print("");

}

System.out.print("");

}

//关闭它,否则会有内存泄露

workbook.close();

⑸ java代码怎么实现打开文件(.xls .doc .txt)

java有个类Desktop(java.awt.Desktop),可以满足你的需求,比如下面的代码:try { Desktop.getDesktop().open(new File("D:\\1.txt"));} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();}路径为本地磁盘的全路径,就可以打开D盘下的1.txt,希望能帮到你。

⑹ java打开excel

在JSP页面中输入如下语句就可以了 :<% //设置文件格式和文件名 response.setContentType("Application/msexcel;charset=GBK"); response.setHeader("Content-disposition", "attachment; filename=Allocate Supply by National MAR or Region.xls"); %>

⑺ java如何读取整个excel文件的内容

在Java中读取Excel文件的内容

在这里,我使用的是一个叫Java Excel API的东西,类似的还有jakarta的POI,不过感觉那个

太复杂了点儿。而且jxl对中文的支持相当的好,至少我在用的过程中一点问题没出。

一、下载地址

http://www.andykhan.com/jexcelapi/

二、特性

可以读取Excel 95, 97, 2000文件

可以读或写Excel 97及其以后版本的的公式(不过我发现好像有bug)

生成Excel 97格式的电子表格

支持字体、数字和日期格式化

支持单元格的颜色和阴影

可以编辑现有的文件

三、读文件

//声明一下,记得后面要关闭哦。。

Workbook workbook = null;

try {

workbook = Workbook.getWorkbook(new File("d:\temp\TestRead.xls"));

} catch (Exception e) {

throw new Exception("file to import not found!");

}

Sheet sheet = workbook.getSheet(0);

Cell cell = null;

int columnCount=3;

int rowCount=sheet.getRows();

for (int i = 0; i<rowcount; p="" {

for (int j = 0; j<columncount; p="" {

//注意,这里的两个参数,第一个是表示列的,第二才表示行

cell=sheet.getCell(j, i);

//要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确

if(cell.getType()==CellType.NUMBER){

System.out.print(((NumberCell)cell).getValue());

}

else if(cell.getType()==CellType.DATE){

System.out.print(((DateCell)cell).getDate());

}

else{

System.out.print(cell.getContents());

}

//System.out.print(cell.getContents());

System.out.print("");

}

System.out.print("");

}

//关闭它,否则会有内存泄露

workbook.close();

⑻ 用java打开一个excel文件时出现的问题。

("cmd.exe /c start C:\\Users\\huangf\\Desktop\\表格.xlsx"); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

⑼ 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 ExcelHandle {public static void main(String[] args) throws Exception {File file = new File("sss.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));// 打开HSSFWorkbookPOIFSFileSystem 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); 3.2已经自动Unicode处理了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);}}给你个例子代码,应该能帮到你!

⑽ java中怎么读取excel文件

package com.jqgj.test;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.commons.io.FilenameUtils;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellValue;import org.apache.poi.ss.usermodel.DateUtil;import org.apache.poi.ss.usermodel.FormulaEvaluator;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 ImportNameTest {/*** Excel 2003*/private final static String XLS = "xls";/*** Excel 2007*/private final static String XLSX = "xlsx";/*** 分隔符*/private final static String SEPARATOR = "|";/*** 由Excel文件的Sheet导出至List* * @param file* @param sheetNum* @return*/public static List<String> exportListFromExcel(File file, int sheetNum)throws IOException {return exportListFromExcel(new FileInputStream(file),FilenameUtils.getExtension(file.getName()), sheetNum);}/*** 由Excel流的Sheet导出至List* * @param is* @param extensionName* @param sheetNum* @return* @throws IOException*/public static List<String> exportListFromExcel(InputStream is,String extensionName, int sheetNum) throws IOException {Workbook workbook = null;if (extensionName.toLowerCase().equals(XLS)) {workbook = new HSSFWorkbook(is);} else if (extensionName.toLowerCase().equals(XLSX)) {workbook = new XSSFWorkbook(is);}return exportListFromExcel(workbook, sheetNum);}/*** 由指定的Sheet导出至List* * @param workbook* @param sheetNum* @return* @throws IOException*/private static List<String> exportListFromExcel(Workbook workbook,int sheetNum) {Sheet sheet = workbook.getSheetAt(sheetNum);// 解析公式结果FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();List<String> list = new ArrayList<String>();int minRowIx = sheet.getFirstRowNum();int maxRowIx = sheet.getLastRowNum();for (int rowIx = minRowIx; rowIx <= maxRowIx; rowIx++) {Row row = sheet.getRow(rowIx);StringBuilder sb = new StringBuilder();short minColIx = row.getFirstCellNum();short maxColIx = row.getLastCellNum();for (short colIx = minColIx; colIx <= maxColIx; colIx++) {Cell cell = row.getCell(new Integer(colIx));CellValue cellValue = evaluator.evaluate(cell);if (cellValue == null) {continue;}// 经过公式解析,最后只存在Boolean、Numeric和String三种数据类型,此外就是Error了// 其余数据类型,根据官方文档,完全可以忽略http://poi.apache.org/spreadsheet/eval.htmlswitch (cellValue.getCellType()) {case Cell.CELL_TYPE_BOOLEAN:sb.append(SEPARATOR + cellValue.getBooleanValue());break;case Cell.CELL_TYPE_NUMERIC:// 这里的日期类型会被转换为数字类型,需要判别后区分处理if (DateUtil.isCellDateFormatted(cell)) {sb.append(SEPARATOR + cell.getDateCellValue());} else {//把手机号码转换为字符串DecimalFormat df = new DecimalFormat("#");sb.append(SEPARATOR + df.format(cellValue.getNumberValue()));}break;case Cell.CELL_TYPE_STRING:sb.append(SEPARATOR + cellValue.getStringValue());break;case Cell.CELL_TYPE_FORMULA:break;case Cell.CELL_TYPE_BLANK:break;case Cell.CELL_TYPE_ERROR:break;default:break;}}list.add(sb.toString());}return list;}/*** @param args*/public static void main(String[] args) {String path = "f:\\telName.xlsx";try {List<String> listS= exportListFromExcel(new File(path),0);for(int i=0;i<listS.size();i++){System.out.println(listS.get(i));}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

未经允许不得转载:山九号 » java打开文件excel|java如何读取整个excel文件的内容

赞 (0)