Action类
ImportDataService service = (ImportDataService)ApplicationFactory.getService("ImportDataService");
String str = "ASSETNAME,ASSETINFORMATION,ASSETTYPE,BUYINGTIME,AMOUNT,USEDDEPT,LOCATION,REMARK,ASSETSTATUS,RESPONSIBLEPERSON" ;
for(int i=1;i<28;i++)
{
str = str+"clo"+i+",";
}
System.out.println(str);
String[] cloName = str.split(",");
service.importData(0, "E://work/医药价格/test.xls", cloName);
service类
public void importMenberData(int sheetNum, String fileName, String[] cloName)
throws FileNotFoundException, IOException, ParseException
{
List listData = readPrice(sheetNum, fileName, cloName);
ContactService service = (ContactService) ApplicationFactory
.getService("ContactService");
for (int i = 0; listData != null && i < listData.size(); i++)
{
Map dataMap = (Map) listData.get(i);
Map result = new HashMap();
MapUtil.addMap(dataMap, result, cloName);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
result.put("JOINDATE",sdf.parse(result.get("JOINDATE").toString()));
result.put("UPDATEDATE",sdf.parse(result.get("UPDATEDATE").toString()));
service.insertMManagement(result);
}
}
public void importData(int sheetNum, String fileName, String[] cloName)
throws FileNotFoundException, IOException
{
List listData = readPrice(sheetNum, fileName, cloName);
String[] paras = { "ASSETNAME", "ASSETINFORMATION", "ASSETTYPE", "BUYINGTIME", "AMOUNT",
"USEDDEPT", "LOCATION", "REMARK", "ASSETSTATUS", "RESPONSIBLEPERSON" };
AdminManagerService service = (AdminManagerService) ApplicationFactory
.getService("AdminManagerService");
for (int i = 0; listData != null && i < listData.size(); i++)
{
Map dataMap = (Map) listData.get(i);
Map result = new HashMap();
MapUtil.addMap(dataMap, result, paras);
service.insertAssetInfo(result);
}
}
/** 读取excel信息,以List返回 */
public List readPrice(int sheetNum, String fileName, String[] cloName)
throws FileNotFoundException, IOException
{
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(sheetNum);
// 获得列,宽值
int rowNum = sheet.getLastRowNum();
int cloNum = sheet.getRow(3).getLastCellNum();
List list = new ArrayList();
// 单元个的值
String cellValue = "";
System.out.println(rowNum + "=========" + cloNum);
for (int rowIndex = 2; rowIndex <= rowNum; rowIndex++)
{
Map mapData = new HashMap();
HSSFRow row = sheet.getRow(rowIndex);
for (int cloIndex = 1; cloIndex < cloNum; cloIndex++)
{
cellValue = cellValue2Str(row.getCell((short) cloIndex));
mapData.put(cloName[cloIndex - 1], cellValue);
}
list.add(mapData);
}
return list;
}
/** 获得HSSFSheet */
private HSSFSheet getSheet(int sheetNum, String fileName) throws FileNotFoundException,
IOException
{
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(sheetNum);
return sheet;
}
/** 获得单元格值 */
private HSSFCell getCellValue(HSSFRow row, int cellIndex)
{
return row.getCell((short) cellIndex++);
}
/** 将单元格值转换为字符型 */
public static String cellValue2Str(HSSFCell cell)
{
String cellValue = "";
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC:
{
cellValue = checkNumNull(cell);
break;
}
case HSSFCell.CELL_TYPE_STRING:
{
cellValue = checkStrNull(cell);
break;
}
default:
{
cellValue = checkDateNull(cell);
// System.out.println("日期是: " + cellValue);
}
}
return cellValue;
}
static String oldValue = "";
/** 检查单元格是否为空 */
public static String checkNumNull(HSSFCell cell)
{
String cellValue = "";
if (cell.getNumericCellValue() == 0)
{
cellValue = oldValue;
}
else
{
cellValue = num2str(cell.getNumericCellValue());
oldValue = cellValue;
}
return cellValue;
}
public static String checkStrNull(HSSFCell cell)
{
String cellValue = "";
if (cell.getStringCellValue().equals(null) || cell.getStringCellValue().equals(""))
{
cellValue = oldValue;
}
else
{
cellValue = cell.getStringCellValue();
oldValue = cellValue;
}
return cellValue;
}
public static String checkDateNull(HSSFCell cell)
{
String cellValue = "";
if (cell.getDateCellValue() == null)
{
cellValue = oldValue;
}
else
{
cellValue = date2str(cell.getDateCellValue());
oldValue = cellValue;
}
return cellValue;
}
/** 数字转换为字符 */
public static String num2str(double cellValue)
{
DecimalFormat df = new DecimalFormat("0.00");
return df.format(new Double(cellValue));
}
/** 日期转换为字符 */
public static String date2str(Date cellValue)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(cellValue);
}
分享到:
相关推荐
POI导入Excel并返回校验后的错误文件(原样数据文件,并添加批注,注:由于批注只能加1000条,会在Excel后面添加一栏错误信息)下载以及页面展示校验错误信息,同时添加导入进度条,提供页面js和css代码,后端...
poi就是批量的操作文件或数据的导入以及导出。,poi导入excel表需要的jar
这个是使用java语言 poi导入excel到数据库中,同时使用了xml对excel的每个单元格数据进行了非空验证。经测试,导入数据库成功。
POI导入Excel,详细内容查看博客 POI导入Excel文件--form表单提交 内容
POI把Excel表格数据导入到数据库的小例子
java_poi实现excel导入导出功能,有详细的注解
POI 导入Excel 提醒LeftoverDataException求帮助
SpringMvc+POI 导入Excel文件,采用简单的form表单提交方式和Jquery.form.js插件提供的异步表单方式导入Excel数据
POI导入excel大数据处理,支持excel2003,2007, 有例子,可以直接运行
poi导入excel 兼容2003-2007兼容版本,测试可以成功;poi导入excel 兼容2003-2007兼容版本,测试可以成功;
NULL 博文链接:https://dengwenjun.iteye.com/blog/1903904
java导入excel的工具类。支持单个对象的赋值,列表对象的赋值,指定坐标的赋值。主要利用的是poi的技术,java反射和自定义注解
java导入excel的工具类。支持单个对象的赋值,列表对象的赋值,指定坐标的赋值。主要利用的是poi的技术,java反射和自定义注解。 20170915,解决了不能导入带有公式字段的excel的问题
通用的POI导入excel解决方法,方便,简易,内附jar包
poi excel 导入 导出poi excel 导入 导出poi excel 导入 导出poi excel 导入 导出poi excel 导入 导出poi excel 导入 导出
poi导入excel示例文件.xlsx
主要是springboot+myBatis+poi+mysql的使用,从数据库查询到结果集导出excel到本地,从本地中的excel文件导入到数据库中。
但导入Excel相对麻烦点,一般情况下,导入的程序无法通用的主要原因是:对于不同的导入模板,字段field与单元格cellRef的映射关系是不相同的,这就导致对每一个导入模板都要去写一套相似的代码;我们应该想办法解决...
poi导入excel示例文件.xlsx