这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx。
读取excel和MySQL相关: java的poi技术读取Excel数据到MySQL
代码如下
/
/
package comb510common;
/
@author Hongten
@created 2014-5-21
/
public class Common {
public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";
public static final String EMPTY = "";
public static final String POINT = "";
public static final String LIB_PATH = "lib";
public static final String STUDENT_INFO_XLS_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2003_POSTFIX;
public static final String STUDENT_INFO_XLSX_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2010_POSTFIX;
public static final String NOT_EXCEL_FILE = " : Not the Excel file!";
public static final String PROCESSING = "Processing";
}
/
/
package comb510excel;
import javaioFileInputStream;
import javaioIOException;
import javaioInputStream;
import javautilArrayList;
import javautilList;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
import orgapachepoixssfusermodelXSSFCell;
import orgapachepoixssfusermodelXSSFRow;
import orgapachepoixssfusermodelXSSFSheet;
import orgapachepoixssfusermodelXSSFWorkbook;
import comb510commonCommon;
import comb510excelutilUtil;
import comb510excelvoStudent;
/
@author Hongten
@created 2014-5-20
/
public class ReadExcel {
/
read the Excel file
@param path the path of the Excel file
@return
@throws IOException
/
public List<Student> readExcel(String path) throws IOException {
if (path == null || CommonEMPTYequals(path)) {
return null;
} else {
String postfix = UtilgetPostfix(path);
if (!CommonEMPTYequals(postfix)) {
if (CommonOFFICE_EXCEL_2003_POSTFIXequals(postfix)) {
return readXls(path);
} else if (CommonOFFICE_EXCEL_2010_POSTFIXequals(postfix)) {
return readXlsx(path);
}
} else {
Systemoutprintln(path + CommonNOT_EXCEL_FILE);
}
}
return null;
}
/
Read the Excel 2010
@param path the path of the excel file
@return
@throws IOException
/
public List<Student> readXlsx(String path) throws IOException {
Systemoutprintln(CommonPROCESSING + path);
InputStream is = new FileInputStream(path);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// Read the Sheet
for (int numSheet = 0; numSheet < xssfWorkbookgetNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbookgetSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= xssfSheetgetLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheetgetRow(rowNum);
if (xssfRow != null) {
student = new Student();
XSSFCell no = xssfRowgetCell(0);
XSSFCell name = xssfRowgetCell(1);
XSSFCell age = xssfRowgetCell(2);
XSSFCell score = xssfRowgetCell(3);
studentsetNo(getValue(no));
studentsetName(getValue(name));
studentsetAge(getValue(age));
studentsetScore(FloatvalueOf(getValue(score)));
listadd(student);
}
}
}
return list;
}
/
Read the Excel 2003-2007
@param path the path of the Excel
@return
@throws IOException
/
public List<Student> readXls(String path) throws IOException {
Systemoutprintln(CommonPROCESSING + path);
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// Read the Sheet
for (int numSheet = 0; numSheet < hssfWorkbookgetNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbookgetSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= hssfSheetgetLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheetgetRow(rowNum);
if (hssfRow != null) {
student = new Student();
HSSFCell no = hssfRowgetCell(0);
HSSFCell name = hssfRowgetCell(1);
HSSFCell age = hssfRowgetCell(2);
HSSFCell score = hssfRowgetCell(3);
studentsetNo(getValue(no));
studentsetName(getValue(name));
studentsetAge(getValue(age));
studentsetScore(FloatvalueOf(getValue(score)));
listadd(student);
}
}
}
return list;
}
@SuppressWarnings("static-access")
private String getValue(XSSFCell xssfRow) {
if (xssfRowgetCellType() == xssfRowCELL_TYPE_BOOLEAN) {
return StringvalueOf(xssfRowgetBooleanCellValue());
} else if (xssfRowgetCellType() == xssfRowCELL_TYPE_NUMERIC) {
return StringvalueOf(xssfRowgetNumericCellValue());
} else {
return StringvalueOf(xssfRowgetStringCellValue());
}
}
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCellgetCellType() == hssfCellCELL_TYPE_BOOLEAN) {
return StringvalueOf(hssfCellgetBooleanCellValue());
} else if (hssfCellgetCellType() == hssfCellCELL_TYPE_NUMERIC) {
return StringvalueOf(hssfCellgetNumericCellValue());
} else {
return StringvalueOf(hssfCellgetStringCellValue());
}
}
}
/
/
package comb510excelclient;
import javaioIOException;
import javautilList;
import comb510commonCommon;
import comb510excelReadExcel;
import comb510excelvoStudent;
/
@author Hongten
@created 2014-5-21
/
public class Client {
public static void main(String[] args) throws IOException {
String excel2003_2007 = CommonSTUDENT_INFO_XLS_PATH;
String excel2010 = CommonSTUDENT_INFO_XLSX_PATH;
// read the 2003-2007 excel
List<Student> list = new ReadExcel()readExcel(excel2003_2007);
if (list != null) {
for (Student student : list) {
Systemoutprintln("No : " + studentgetNo() + ", name : " + studentgetName() + ", age : " + studentgetAge() + ", score : " + studentgetScore());
}
}
Systemoutprintln("======================================");
// read the 2010 excel
List<Student> list1 = new ReadExcel()readExcel(excel2010);
if (list1 != null) {
for (Student student : list1) {
Systemoutprintln("No : " + studentgetNo() + ", name : " + studentgetName() + ", age : " + studentgetAge() + ", score : " + studentgetScore());
}
}
}
}
/
/
package comb510excelutil;
import comb510commonCommon;
/
@author Hongten
@created 2014-5-21
/
public class Util {
/
get postfix of the path
@param path
@return
/
public static String getPostfix(String path) {
if (path == null || CommonEMPTYequals(pathtrim())) {
return CommonEMPTY;
}
if (pathcontains(CommonPOINT)) {
return pathsubstring(pathlastIndexOf(CommonPOINT) + 1, pathlength());
}
return CommonEMPTY;
}
}
/
/
package comb510excelvo;
/
Student
@author Hongten
@created 2014-5-18
/
public class Student {
/
id
/
private Integer id;
/
学号
/
private String no;
/
姓名
/
private String name;
/
学院
/
private String age;
/
成绩
/
private float score;
public Integer getId() {
return id;
}
public void setId(Integer id) {
thisid = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
thisno = no;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
thisage = age;
}
public float getScore() {
return score;
}
public void setScore(float score) {
thisscore = score;
}
}
int realRowCount = sheetgetPhysicalNumberOfRows();/ 获取有数据的行数,即:最后有数据的行是第n行,前面有m行是空行没数据,则返回n-m /
int rowIndex=0; / 行号下标,从0开始 /
Row row = null;
for (int j = 0; j < realRowCount ; j++) {
row = sheetgetRow(rowIndex++);
if(row !=null){
/ 处理数据 /
}else{
j--;
}
}
if(rowIndex>0){
rowIndex--;
}
Excel中部分内容有问题,是否让我们尝试恢复,如果信任此工作簿的源请单击是是设置错误造成的,解决方法为:
1、首先,打开表格点击“文件”点击“选项” 。
2、找到并点击“高级”。
3、之后,在高级中找到“此工作部的显示选项”,查看“显示工作表标签”是否勾选,若无勾选,请选择 。
4、最后,点击“确定”即可。
两个原因:
1你的excel模版本身有问题,可以尝试新建一个模版。
2你的excel使用了一些POI不支持的函数。
解决办法:
另存是由excel重写了完整的文件,可以解决问题。
关闭文件例子:
FileOutputStream os = new FileOutputStream("workbookxls");
wbwrite(os);
osclose();
在保护状态下execl的格式有可能正在被使用,你这边修改,准确说是线程冲突,一般excel值会作为导出文件的模板,是不会编辑的。你可以在读的时候判断execl是否正在被使用。
下面的代码问题,你可以参考
package comhwtglmfcommon;
import javaioIOException;
import javaioOutputStream;
import javautilArrayList;
import javautilList;
import javaxservlet>
合并单元格出现不可读取内容,即 合并单元格引起了excel软件读取EXCEL文件异常
距离如下:
假设F50到 I50已经合并过, J50:K50合并过,再去合并就会出现提示:
$phpexcel->getActiveSheet()->unmergeCells("F50:I50")->unmergeCells("J50:K50")->mergeCells("F50:K50")->setCellValue("F50","Shipper's enterprise code (TAX ID) incl identifier:91440000721185260Y");
解决办法就是 先把所有合并的 F50:I50,J50:K50先解除单元格合并,再按实际合并,则不会报错。
扩展资料:
PHPExcel读取代码:
//获取上传的excel临时文件$path = $_FILES["file"]["tmp_name"];
//将临时文件移动当前目录,可自定义存储位置 move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
//将获取在服务器中的Excel文件,此处为上传文件名$path = $_FILES["file"]["name"];
//调用readExcel函数返回一个二维数组$exceArray = readExcel($path);
//创建一个读取excel函数function readExcel($path){
//引入PHPExcel类库 include 'Classes/PHPExcelphp'; nclude 'Classes/PHPExcel/IOFactoryphp'; $type = 'Excel5';
//设置为Excel5代表支持2003或以下版本,Excel2007代表2007版 $xlsReader = \PHPExcel_IOFactory::createReader($type); $xlsReader->setReadDataOnly(true); $xlsReader->setLoadSheetsOnly(true); $Sheets = $xlsReader->load($path);
//开始读取上传到服务器中的Excel文件,返回一个二维数组 $dataArray = $Sheets->getSheet(0)->toArray(); return $dataArray;
}
参考资料来源:百度百科-phpexcel
以上就是关于Java *** 作Excel文件时,如何设置cell内的文字居中全部的内容,包括:Java *** 作Excel文件时,如何设置cell内的文字居中、java 如何 *** 作excel 插入一列、Java对Excel解析(求助)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)