java导入.xls .xslx .txt三种后缀格式的文件
public static void readFile(File file) throws IOException{
//判断文件类型
String fileName = file.getName();
fileName = fileName.toLowerCase();
if(fileName.endsWith("xls")){
readXls(file);
}else if(fileName.endsWith("txt")){
readTxt(file);
}else if(fileName.endsWith("xlsx")){
readXlsx(file);
}else{
throw new IOException("不支持文件类型");
}
}
//导入2003 xls文件
private static void readXls(File file)throws IOException{
String[] keys ={ "content","mobile","ext","status","responseid","error","sendtime","bak1","bak2","bak3","bak4"};
HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));
int length = hwb.getActiveSheetIndex();//判断有几张活动的sheet表
for(int k=0; k<=length; k++){
HSSFSheet sheet = hwb.getSheetAt(k);
Object value = null;
HSSFRow row = null;
HSSFCell cell = null;
List<Dto> lists=null;
for(int i=sheet.getFirstRowNum()+1; i<sheet.getPhysicalNumberOfRows(); i++){
row = sheet.getRow(i);
if(row == null){
continue;
}
if(i%1000==1){
lists=new ArrayList<Dto>();
}
Map hashmap = new HashMap();
for(int j=row.getFirstCellNum(); j<row.getLastCellNum(); j++){
cell = row.getCell(j);
if(cell == null){
value = "";
hashmap.put(keys[j], value);
continue;
}
DecimalFormat df = new DecimalFormat("0");//格式化number String
//字符
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化日期字符串
DecimalFormat nf = new DecimalFormat("0");//格式化数字
switch(cell.getCellType()){
case XSSFCell.CELL_TYPE_STRING:
//System.out.println(i+"行"+j+"列 is String type" );
value = cell.getStringCellValue();
//System.out.println(" "+ value +" ");
break;
case XSSFCell.CELL_TYPE_NUMERIC:
//System.out.println(i+"行"+j+"列 is Number type; DateFormt:" +cell.getCellStyle().getDataFormatString());
if("@".equals(cell.getCellStyle().getDataFormatString())){
value = df.format(cell.getNumericCellValue());
}else if("General".equals(cell.getCellStyle().getDataFormatString())){
value = nf.format(cell.getNumericCellValue());
}else{
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}
//System.out.println(" "+value+" ");
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
//System.out.println(i+"行"+j+"列 is Boolean type");
value = cell.getBooleanCellValue();
//System.out.println(" "+value+" ");
break;
case XSSFCell.CELL_TYPE_BLANK:
//System.out.println(i+"行"+j+"列 is Blank type");
value = "";
//System.out.println(" "+value+" ");
break;
default:
//System.out.println(i+"行"+j+"列 is default type");
value = cell.toString();
//System.out.println(" "+value+" ");
}
hashmap.put(keys[j],value);
}
//将数据插入数据库
Dto fileDto = new BaseDto();
for (int j = 0; j < keys.length; j++) {
if (hashmap.get(keys[j]) == null
|| "".equals(hashmap.get(keys[j]))) {
if ("sendtime".equals(keys[j])) {
fileDto.put(keys[j], s_date);
} else {
fileDto.put(keys[j], "");
}
} else {
fileDto.put(keys[j], hashmap.get(keys[j]));
}
}
lists.add(fileDto);
if(i%1000==0 || i==sheet.getPhysicalNumberOfRows()-1){
myBatchService.insertBatchdx(lists);
}
// cetReadFilesService.insertFile(fileDto);
}
}
}
//导入2007 xlsx文件
private static void readXlsx(File file) throws IOException{
String[] keys = { "content","mobile","ext","status","responseid","error","sendtime","bak1","bak2","bak3","bak4"};
//购置XXSFWorkbook对象,传入file文件
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
//格式化日期字符串
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
//读取第一张表格内容
int length = xwb.getActiveSheetIndex();
for(int k=0; k<=length; k++){
XSSFSheet sheet = xwb.getSheetAt(0);
Object value = null;
XSSFRow row = null;
XSSFCell cell = null;
List<Dto> lists=null;
for(int i=sheet.getFirstRowNum()+1; i<sheet.getPhysicalNumberOfRows(); i++){
row = sheet.getRow(i);
if(row==null){
continue;
}
if(i%1000==1){
lists=new ArrayList<Dto>();
}
Map hashmap = new HashMap();
for(int j=row.getFirstCellNum(); j<row.getLastCellNum(); j++){
cell = row.getCell(j);
if(cell == null){
value = "";
hashmap.put(keys[j], value);
continue;
}
DecimalFormat df = new DecimalFormat("0");//格式化 number String
//字符
DecimalFormat nf = new DecimalFormat("0");//格式化数字
switch(cell.getCellType()){
case XSSFCell.CELL_TYPE_STRING:
//System.out.println(i+"行"+j+"列 is String type" );
value = cell.getStringCellValue();
//System.out.println(" "+ value +" ");
break;
case XSSFCell.CELL_TYPE_NUMERIC:
//System.out.println(i+"行"+j+"列 is Number type; DateFormt:" +cell.getCellStyle().getDataFormatString());
if("@".equals(cell.getCellStyle().getDataFormatString())){
value = df.format(cell.getNumericCellValue());
}else if("General".equals(cell.getCellStyle().getDataFormatString())){
value = nf.format(cell.getNumericCellValue());
}else{
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}
//System.out.println(" "+value+" ");
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
//System.out.println(i+"行"+j+"列 is Boolean type");
value = cell.getBooleanCellValue();
//System.out.println(" "+value+" ");
break;
case XSSFCell.CELL_TYPE_BLANK:
//System.out.println(i+"行"+j+"列 is Blank type");
value = "";
//System.out.println(" "+value+" ");
break;
default:
//System.out.println(i+"行"+j+"列 is default type");
value = cell.toString();
//System.out.println(" "+value+" ");
}
hashmap.put(keys[j], value);
}
//将数据插入数据库
Dto fileDto = new BaseDto();
for (int j = 0; j < keys.length; j++) {
if (hashmap.get(keys[j]) == null
|| "".equals(hashmap.get(keys[j]))) {
if ("sendtime".equals(keys[j])) {
fileDto.put(keys[j], s_date);
} else {
fileDto.put(keys[j], "");
}
} else {
fileDto.put(keys[j], hashmap.get(keys[j]));
}
}
lists.add(fileDto);
if(i%1000==0 || i==sheet.getPhysicalNumberOfRows()-1){
myBatchService.insertBatchdx(lists);
}
}
}
}
/**
* 导入TXT文件
*/
private static void readTxt(File file) {
//count用于计数
int count = 0;
String[] keys = {"content","mobile","ext","status","responseid","error","sendtime","bak1","bak2","bak3","bak4"};
FileReader reader;
List<Dto> lists=null;
try {
reader = new FileReader(file);
BufferedReader bf=new BufferedReader(reader);//一行一行读
try {
String t1=bf.readLine();
while(t1 != null){
count++;
if(count%1000==1){
lists=new ArrayList<Dto>();
}
String[] strs = t1.split("\\|");
Map hashmap = new HashMap();
for(int i=0; i<strs.length; i++){
hashmap.put(keys[i], strs[i]);
}
//将数据插入数据库
if(count != 1){
Dto fileDto = new BaseDto();
for (int j = 0; j < keys.length; j++) {
if (hashmap.get(keys[j]) == null
|| "".equals(hashmap.get(keys[j]))) {
if ("sendtime".equals(keys[j])) {
fileDto.put(keys[j], s_date);
} else {
fileDto.put(keys[j], "");
}
} else {
fileDto.put(keys[j], hashmap.get(keys[j]));
}
}
lists.add(fileDto);
if(count%1000==0 ){
myBatchService.insertBatchdx(lists);
}
}
t1=bf.readLine();
if(t1==null){
myBatchService.insertBatchdx(lists);
}
}
}catch (IOException e){
e.printStackTrace();
}
}catch(FileNotFoundException e){
e.printStackTrace();
}
}
分享到:
相关推荐
为了减轻服务器的压力,很多网站采用的js下载或导出数据的方式...该文件就是利用javascript把json数据转换成excel数据后,并进行了下载。 真实可用,而且使用起来方便快捷,为了开发快速,可以下载看看。 里面有教程的
XLS与XLSX互相转换,在2003的EXCEL创造的表格在2007版本中打不开的情况下,这是一个利器。
如何将excel.xslx表格数据导出成Visual Fox(VF)表.zip
此表格收集了中国历年各省GDP数据,共27年的数据,可作为数据训练、绘制图形的基本数据,文件为表格,可以直接用于复制、粘贴,方便快捷。
easyPoi模板导出Excel报表(xls 和xlsx 都支持)
OpenXLSX是一套开源的读、写、创建、修改Excel文件(格式:xlsx)的C++库。附件内容:源码+示例。
xslx)导入CouchDB / PouchDB文档,并将工作表转换为JSON。 用法 文档更新冲突是可能的,将被忽略。 excel-couchdb-import path/to/file.xls \ --target http://user:password@couchdbhost.com/targetdb 重新导入 ...
由于项目需要同时导入excel文档和jpg图片,因此尝试了一些方法来完成任务。 图片数据是以base64格式存入的,显示和转移都比较方便。 一开始考虑的是将图片插入excel中,这样的好处是一一对应,不会出错,但是搜索了...
ASP.NET导入导出excel文件数据,可以实现03和07版的excel文件的导入导出
couchdb-excel-import 将Excel工作表导入CouchDB couchdb-excel-import Excel文件(* .xls,*。xslx)导入CouchDB文档,并将工作表转换为JSON。用法couchdb-excel-import path/to/file.xls \ --target ...
读取xls、xlsx文件所需jar包.................................................................................................................................................................................
前端导出excel,可以控制样式,单元格合并居中等
通过此工具可以更好的在unity中读取配置文件excel格式文件信息,动态修改数据
python3实现xls文件转xlsx文件, 调用系统win32库
这是一个 Go 语言用来读写新的 Excel 文件格式 —— XSLX 的开发包。读示例:package main import ( "fmt" "github.com/tealeg/xlsx" ) func main() { excelFileName := "/home/tealeg/foo.xlsx" xl...
用作前端页面数据的导出,文件包括Blob.js和Blob.js,导出的格式为xslx。
文件中sheet1表格定义数据名和格式(目前格式有num,string,list),sheet2 定义数据内容。注意:list格式的内容可以是数据,也可以是另一个xlsx文件名。xlsx文件都应在一个文件夹中。输入文件名为xlsxTojson.json...
工作交接表.xlsx
这里要用到的两个小插件,一个是xslx.js,一个是FileSaver.js,前者是来处理生成Excel的,后者是用来把文件下载保存到本地的。
这是一个读写excel文件的库,不需要微软的office和.net框架,支持xsl,xslx,xslm,支持64位平台和Unicode。 经测试,已经XXX,c++创建xls 2003版的excel文件,文件的第一行会生成试用版的信息。如果创建xlsx 2007-...