乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > JAVA实现EXCEL表格文件(.xls格式)的读取、修改、...-excel文件标准,excel合并多个文件

JAVA实现EXCEL表格文件(.xls格式)的读取、修改、...-excel文件标准,excel合并多个文件

作者:乔山办公网日期:

返回目录:excel表格制作


参考代码e5a48de588b6e79fa5e98193364
public void getValueOfFormulaCell() throws IOException
{
FileInputStream xlsfile = new FileInputStream(new File("D:\\Desktop\\Temp\\marks.xls"));
HSSFWorkbook objWorkbook = new HSSFWorkbook(xlsfile);
Sheet sheet = objWorkbook.getSheetAt(0);
FormulaEvaluator evaluator = objWorkbook.getCreationHelper().createFormulaEvaluator();

// suppose your formula is in B4
CellReference cellReference = new CellReference("B4");
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());

CellValue cellValue = evaluator.evaluate(cell);

switch (cellValue.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cellValue.getBooleanValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cellValue.getNumberValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println(cellValue.getStringValue());
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
break;

// CELL_TYPE_FORMULA will never happen
case Cell.CELL_TYPE_FORMULA:
break;
}
}

import Java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ReadExcel {
public static void readExcel(File file){
try {
InputStream inputStream = new FileInputStream(file);
String fileName = file.getName();
Workbook wb = null;
// poi-3.9.jar 只可以读取2007以下的7a64e58685e5aeb9365版本,后缀为:xsl
wb = new HSSFWorkbook(inputStream);//解析xls格式

Sheet sheet = wb.getSheetAt(0);//第一个工作表 ,第二个则为1,以此类推...

int firstRowIndex = sheet.getFirstRowNum();
int lastRowIndex = sheet.getLastRowNum();
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){
Row row = sheet.getRow(rIndex);
if(row != null){
int firstCellIndex = row.getFirstCellNum();
// int lastCellIndex = row.getLastCellNum();
//此处参数cIndex决定可以取到excel的列数。
for(int cIndex = firstCellIndex; cIndex < 3; cIndex ++){
Cell cell = row.getCell(cIndex);
String value = "";
if(cell != null){
value = cell.toString();
System.out.print(value+"\t");
}
}
System.out.println();
}
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
File file = new File("D:/test.xls");
readExcel(file);
}
}
public String[][] readExcel(String filePath) {
String[][] s = null;
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
filePath));
HSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
s = new String[rows][];
if (rows > 0) {
// 获取总列数`e68a847a686964616f338
int cells = sheet.getRow(0).getPhysicalNumberOfCells();
for (int r =0; r < rows; r++) {

HSSFRow row = sheet.getRow(r);
String[] cellsvalue = new String[cells+1];
for (short c = 0; c < cells; c++) {
String value = "";
HSSFCell cell = row.getCell(c);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
//
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
value = new java.text.SimpleDateFormat(
"yyyy-MM-dd").format(cell
.getDateCellValue());
} else {
value = String.valueOf(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
value="";
default:
break;
}
if (cell == null) {
value="";
}
}
cellsvalue[c] = value;
if(value.endsWith(".0")){
cellsvalue[c] = value.substring(0, value.length()-2);
}
}
s[r] = cellsvalue;
java.io.File myfile = new java.io.File(filePath);
if (myfile.exists()) {
myfile.delete();
}
}
}
} catch (Exception ex) {
// TODO 自动生成 catch 块

ex.printStackTrace();
}
return s;
}

我也尝试过好多种办法,这个转换不行。应该尝试另存。
见网址:http://universes.iteye.com/blog/854485

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网