1.导入
编写一个excel转换的工具类,把接收到的文件转换为特定类型的数据,如下:
package com.zeyyo.product.elec.comm;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.zeyyo.product.elec.domain.VindexConfig;
public class ExcelUtils {
/**
* 得到Excel,并解析内容
* @param file
* @throws FileNotFoundException
* @throws IOException
* @throws InvalidFormatException
*/
public List<VindexConfig> getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException{
//1.得到Excel常用对象
// POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));
//2.得到Excel工作簿对象
// HSSFWorkbook wb = new HSSFWorkbook(fs);
InputStream ins = null;
Workbook wb = null;
ins=new FileInputStream(new File(file));
//ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath);
try {
wb = WorkbookFactory.create(ins);
} catch (EncryptedDocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ins.close();
//3.得到Excel工作表对象
Sheet sheet = wb.getSheetAt(0);
//总行数
int trLength = sheet.getLastRowNum();
//4.得到Excel工作表的行
// Row row = sheet.getRow(0);
//总列数
// int tdLength = row.getLastCellNum();
//5.得到Excel工作表指定行的单元格
//Cell cell = row.getCell((short)1);
//6.得到单元格样式
// CellStyle cellStyle = cell.getCellStyle();
// for(int i=5;i<trLength;i++){
// //得到Excel工作表的行
// Row row1 = sheet.getRow(i);
// for(int j=0;j<tdLength;j++){
// //得到Excel工作表指定行的单元格
// Cell cell1 = row1.getCell(j);
// /**
// * 为了处理:Excel异常Cannot get a text value from a numeric cell
// * 将所有列中的内容都设置成String类型格式
// */
// if(cell1!=null){
// cell1.setCellType(Cell.CELL_TYPE_STRING);
// }
//
if(j==5&&i<=10){
cell1.setCellValue("1000");
}
//
// //获得每一列中的值
// System.out.print(cell1+" ");
// }
// System.out.println();
// }
//
// //将修改后的数据保存
// OutputStream out = new FileOutputStream(file);
// wb.write(out);
//用于返回结果集
List<VindexConfig> mList = new ArrayList();
//要获得属性
String paramName = "";
String dataType = "";
int vindex = 0;
String parentTypeId = "";
String parentName = "";
int equindex = 0;
String nodeType = "";
String memo = "";
String stationName = "";
int stationId = 0;
//获得所有数据
for(int i = 1 ; i <= trLength ; i++)
{
//获得第i行对象
Row row = sheet.getRow(i);
//获得第i行第0列的 String类型对象
Cell cell = row.getCell((short)0);
paramName = cell.getStringCellValue().toString();
cell = row.getCell((short)1);
dataType = cell.getStringCellValue().toString();
//获得一个数字类型的数据
cell = row.getCell((short)2);
vindex = (int) cell.getNumericCellValue();
cell = row.getCell((short)3);
parentTypeId = cell.getStringCellValue().toString();
cell = row.getCell((short)4);
parentName = cell.getStringCellValue().toString();
cell = row.getCell((short)5);
equindex = (int) cell.getNumericCellValue();
cell = row.getCell((short)6);
nodeType = cell.getStringCellValue().toString();
cell = row.getCell((short)7);
if(cell!=null){
memo = cell.getStringCellValue().toString();
}else{
memo="";
}
cell = row.getCell((short)8);
stationName = cell.getStringCellValue().toString();
cell = row.getCell((short)9);
stationId = (int) cell.getNumericCellValue();
//将数据封装为一个对象
VindexConfig vc = new VindexConfig();
vc.setParamName(paramName);
vc.setDataType(dataType);
vc.setVindex(vindex);
vc.setParentTypeId(parentTypeId);
vc.setParentName(parentName);
vc.setEquindex(equindex);
vc.setNodeType(nodeType);
vc.setMemo(memo);
vc.setStationName(stationName);
vc.setStationId(stationId);
//将对象放入结果集
mList.add(vc);
//System.out.println("参数名称:"+paramName+",数据类型:"+dataType+",点位:"+vindex+",设备标识:"+parentTypeId+",设备名称:"+parentName+",设备编号:"+equindex+",节点类型:"+nodeType+",备注:"+memo+",变电所名称:"+stationName+",变电所id:"+stationId);
}
return mList;
}
}
只需要传入文件路径,并且excel文件内的数据格式符合要求即可批量导入数据。在前端页面选择文件获取文件绝对路径时,因为安全性问题,目前只有IE内核的浏览器可以获取绝对路径,其他内核的浏览器一般会给出一个假路径,如果使用360浏览器,切换至兼容模式即可获取绝对路径。
2.导出
一般项目中要求导出的数据只是当前显示的表格数据,很少有要求导出全部数据库数据的情况,所以下面说一下导出前端表格数据并保存为excel文件的方法,方法其实非常简单:
首先在项目中导入以下两个文件:
引用两个文件:
<script type="text/javascript"
src="/lib/jquery/jquery-export/tableExport.min.js"></script>
<script type="text/javascript"
src="/lib/jquery/jquery-export/jquery.base64.js"></script>
在html页面写一个按钮用来触发事件:
<button class="export" type="button" id="BtnExport">导出</button>
js页面编写导出方法设置属性(两个传参为要导出数据的表格对象和自定义的表格名称):
function exportTable($table,name){
$table.tableExport({
type:'excel',
separator:';',
escape:'false',
fileName:excelName
});
}
设置button点击触发事件:
$("#BtnExport").click(function(){
var name = “表格名称”;
exportTable($("#tableData"),name);
});
这样点击按钮后即可导出excel格式的表格数据。