后台代码数据解析:
方法一: (简单点)
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;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 读取Excel
* @author wf
*/
public class ReadExcelUtils {
/**
* 解析xlsx,xls
* @param filepath
* @return
* @throws IOException
*/
public void ReadExcelXlsx(String filepath) throws IOException {
File files = new File(filepath);
InputStream isd = new FileInputStream(files);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(isd);
// 获取每一个工作薄
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
// 获取当前工作薄的每一行
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
BudgetAssessManagement em = new BudgetAssessManagement();
if (xssfRow != null) {
System.out.println(xssfRow.getCell(0));
System.out.println(xssfRow.getCell(1));
...
}
}
}
}
//转换数据格式
public String getValue(XSSFCell xssfRow) {
if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
return String.valueOf(xssfRow.getBooleanCellValue());
} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
return String.valueOf(xssfRow.getNumericCellValue());
} else {
return String.valueOf(xssfRow.getStringCellValue());
}
}
/**
* 解析xls
* @param filepath
* @return
* @throws IOException
*/
public String ReadExcelUtilsdd(String filepath) throws IOException {
InputStream is = new FileInputStream(filepath);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
// 获取每一个工作薄
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 获取当前工作薄的每一行
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
HSSFCell one = hssfRow.getCell(0);
//读取第一列数据
HSSFCell two = hssfRow.getCell(1);
//读取第二列数据
HSSFCell three = hssfRow.getCell(2);
//读取第三列数据
}
}
}
return null;
}
// 转换数据格式
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getStringCellValue());
}
}
/* public static void main(String[] args) {
try {
String filepath = "C:\\Users\\LENOVO\\Desktop\\22.xlsx";
// ReadExcelUtils(filepath);
} catch (Exception e) {
System.out.println("未找到指定路径的文件!");
}
}*/
}
所需解析所需jar包
dom4j-1.6.1.jar
poi-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
xmlbeans-2.3.0.jar
方法二:
Excel读取,创建Excel 缺点就是只能操作2007还是2003Excel记不清了。。。试试
package com.yc.qecmm;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
*
* @ClassName: ExcelOperate
* @Description: TODO(使用jxl简单实现Excel导入导出)
* @author wf
*
*/
public class ExcelOperate {
int col=100,row =100;
/**
* @Title: ExcelOperate
* @Description: TODO(读取excel文件里的内容)
* @param @param path 设定文件
* @return @param path 返回类型
* @throws
*/
public List<DrugsEntity> readExcel(HttpServletRequest request,File file){
List<DrugsEntity> drglist = new LinkedList<DrugsEntity>();
//重新获得VariableDrugsEntity放置map中传递到页面
ServletContext contt = request.getServletContext();
VariableDrugsEntity var =(VariableDrugsEntity) contt.getAttribute("v");
StringBuffer sb = new StringBuffer();
try {
Workbook book = Workbook.getWorkbook(file);//拿到excel工作空间
try{
Sheet sheet = book.getSheet(0);
col = book.getSheet(0).getColumns();//获取excel文件有列
row = book.getSheet(0).getRows();//获取excel文件有行
var.setTotle(row - 1);
System.out.println(col+"=="+row);
for(int i = 1 ; i < row ; i++){
DrugsEntity dr =new DrugsEntity();
for(int j = 1 ; j < col ; j++){
//第一个参数代表列,第二个参数代表行。(默认起始值都为0)
try{
Cell jj =sheet.getCell(j, i);//获取excel单个格子
if(jj!=null){
String cont=jj.getContents();//获取格子的内容
if(j==1){
dr.setJhc(cont);
}else if(j==2){
dr.setYoqhs(cont);
}else if(j==3){
dr.setAws(cont);
}else if(j==4){
dr.setHfy(cont);
}else if(j==5){
dr.setGg(cont);
}
sb.append(cont+"\t");
}
/* //判断羌的种类和等级
MathUntile ma = new MathUntile();
//dr = ma.SpecCalculation(dr);
dr = ma.QualityLevel(request,dr);*/
}catch(Exception e){
System.out.println("这个表格没有值");
}
}
drglist.add(dr);
sb.append("\n");
}
}finally{
if(book != null){
book.close();
}
}
} catch (BiffException e) {
System.err.println(e+"");
} catch (IOException e) {
System.err.println(e+"文件读取错误");
}
return drglist;
}//end readFromFile
//往excel中写
public void writeToFile(List<DrugsEntity> list,String path){
File file = new File(path);
try {
WritableWorkbook book = Workbook.createWorkbook(file);
//创建一个工作区。(默认的excel文件有三个sheet,在excel的左下角可以看到sheet1/sheet2/sheet3)
WritableSheet sheet = book.createSheet("第一页", 0);
//在工作区上面添加内容
System.out.println("wowowowo="+col+"=="+row);
try {
for(int i = 0; i < col ; i ++ ){
for(int j = 0 ; j <row ; j++){
Label newLabel =new Label(0,0,"序号");
//第一个参数代表列,第二个参数代表行(默认起始值都为0),
//第三个参数是要在单元格里面填写的内容发
if(j == 0){
if(i==0){
newLabel = new Label(i,j,"序号");
}else if(i==1){
newLabel = new Label(i,j,"表头");
}else if(i==2){
newLabel = new Label(i,j,"表头");
}....等
}else if(j>0){
if(i==0){
newLabel = new Label(i,j,String.valueOf(j));
}else if(i==1){
newLabel = new Label(i,j,list.get(j-1).getQhc());
}else if(i==2){
newLabel = new Label(i,j,list.get(j-1).getYoqhs());
}...等
}
//在单元格上面添加注释
// WritableCellFeatures cellFeatures = new WritableCellFeatures();
// cellFeatures.setComment("这里是"+i+"*"+j+"的值");
// System.out.println("这里是"+i+"*"+j+"的值");
// newLabel.setCellFeatures(cellFeatures);newLabel.
sheet.addCell(newLabel);
}
}
} catch (RowsExceededException e) {
System.err.println(e+"行或列参数错误!");
} catch (WriteException e) {
System.err.println(e+"写入失败");
}finally{
if(book != null){
book.write();
try {
book.close();
} catch (WriteException e) {
System.err.println(e+"文件关闭失败!");
}
}
}
} catch (IOException e) {
System.err.println(e+"创建文件失败!");
}
}
}
注意自己使用时需要修改个别地方
做需要的核心jar包 jxl.jar