入文件若为二进制格式的csv或xls文件需要用jxl.jar包来解析,
若文件为文本格式逗号分隔的csv文件则需要用opencsv.jar包来解析。
如何区分文件为二进制格式,简单的方法可以右键使用记事本打开,若为乱码则为二进制文件,这样的文件只能使用office的excel工具打开;而 文本格式逗号分隔的csv文件使用记事本就可以打开。
下面为导入两种格式文件的代码示例:
1. 导入文本格式逗号分隔的csv文件
- import au.com.bytecode.opencsv.CSVReader;
- import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVReader;
- public void importCsvFile() {
- CSVReader csvReader = null ;
- try {
- csvReader = new CSVReader( new FileReader(importFile), ',' ); //importFile为要导入的文本格式逗号分隔的csv文件,提供getXX /setXX方法
- if (csvReader != null ){
- //first row is title, so past
- csvReader.readNext();
- String[] csvRow = null ; //row
- while ((csvRow = csvReader.readNext()) != null ){
- for ( int i = 0 ; i<csvRow.length; i++){
- String temp = csvRow[i];
- switch (i) {
- case 0 :
- if (StringUtils.isNotEmpty(temp)){
- linkman.setLinkmanName(temp);
- }
- break ;
- case 1 :
- if (StringUtils.isNotEmpty(temp)){
- linkman.setLinkmanEmail(temp);
- }
- break ;
- default :
- break ;
- }
- }
- //保存linkman到数据库
- if (linkman.getLinkmanName() != null && linkman.getLinkmanEmail() != null ){
- EmailLinkmanAPI.insertLinkman(linkman);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void importCsvFile() {
- CSVReader csvReader = null ;
- try {
- csvReader = new CSVReader( new FileReader(importFile), ',' ); //importFile 为要导入的文本格式逗号分隔的csv文件,提供getXX/setXX方法
- if (csvReader != null ){
- //first row is title, so past
- csvReader.readNext();
- String[] csvRow = null ; //row
- while ((csvRow = csvReader.readNext()) != null ){
- for ( int i = 0 ; i<csvRow.length; i++){
- String temp = csvRow[i];
- switch (i) {
- case 0 :
- if (StringUtils.isNotEmpty(temp)){
- linkman.setLinkmanName(temp);
- }
- break ;
- case 1 :
- if (StringUtils.isNotEmpty(temp)){
- linkman.setLinkmanEmail(temp);
- }
- break ;
- default :
- break ;
- }
- }
- //保存linkman到数据库
- if (linkman.getLinkmanName() != null && linkman.getLinkmanEmail() != null ){
- EmailLinkmanAPI.insertLinkman(linkman);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
public void importCsvFile() {
CSVReader csvReader = null;
try {
csvReader = new CSVReader(new FileReader(importFile),',');//importFile为要导入的文本格式逗号分隔的csv文件,提供getXX/setXX方法
if(csvReader != null){
//first row is title, so past
csvReader.readNext();
String[] csvRow = null;//row
while ((csvRow = csvReader.readNext()) != null){
for (int i =0; i<csvRow.length; i++){
String temp = csvRow[i];
switch (i) {
case 0:
if(StringUtils.isNotEmpty(temp)){
linkman.setLinkmanName(temp);
}
break;
case 1:
if(StringUtils.isNotEmpty(temp)){
linkman.setLinkmanEmail(temp);
}
break;
default:
break;
}
}
//保存linkman到数据库
if(linkman.getLinkmanName() != null && linkman.getLinkmanEmail() != null){
EmailLinkmanAPI.insertLinkman(linkman);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
2. 二进制格式的csv或xls文件
- import jxl.Sheet;
- import jxl.Workbook;
- import jxl.Sheet;
- import jxl.Workbook;
import jxl.Sheet;
import jxl.Workbook;
- public void importXlsFile() {
- Workbook book = null ;
- try {
- book = Workbook.getWorkbook(importFile); //importFile为要导入的 xls文件,或二进制的csv文件,提供getXX/setXX方法
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (book != null ){
- int sheetNo = book.getNumberOfSheets();
- for ( int i = 0 ; i < sheetNo; i++){
- Sheet sheet=book.getSheet(i);
- int rowNum = sheet.getRows();
- int colNum = sheet.getColumns();
- //first row is title, so past
- for ( int r = 1 ; r < rowNum; r++){ //行
- for ( int c = 0 ; c < colNum;c++){ //列
- String temp = sheet.getCell(c,r).getContents();
- switch (c) {
- case 0 :
- if (StringUtils.isNotEmpty(temp)){
- linkman.setLinkmanName(temp);
- }
- break ;
- case 1 :
- if (StringUtils.isNotEmpty(temp)){
- linkman.setLinkmanEmail(temp);
- }
- break ;
- default :
- break ;
- }
- }
- //保存linkman到数据库
- if (linkman.getLinkmanName() != null && linkman.getLinkmanEmail() != null ){
- EmailLinkmanAPI.insertLinkman(linkman);
- }
- }
- }
- }
- }
- public void importXlsFile() {
- Workbook book = null ;
- try {
- book = Workbook.getWorkbook(importFile);//importFile为要导入的xls文件,或二进制的csv文件,提供getXX/setXX方法
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (book != null ){
- int sheetNo = book.getNumberOfSheets();
- for ( int i = 0 ; i < sheetNo; i++){
- Sheet sheet=book.getSheet(i);
- int rowNum = sheet.getRows();
- int colNum = sheet.getColumns();
- //first row is title, so past
- for ( int r = 1 ; r < rowNum; r++){ //行
- for ( int c = 0 ; c < colNum;c++){ //列
- String temp = sheet.getCell(c,r).getContents();
- switch (c) {
- case 0 :
- if (StringUtils.isNotEmpty(temp)){
- linkman.setLinkmanName(temp);
- }
- break ;
- case 1 :
- if (StringUtils.isNotEmpty(temp)){
- linkman.setLinkmanEmail(temp);
- }
- break ;
- default :
- break ;
- }
- }
- //保存linkman到数据库
- if (linkman.getLinkmanName() != null && linkman.getLinkmanEmail() != null ){
- EmailLinkmanAPI.insertLinkman(linkman);
- }
- }
- }
- }
- }
public void importXlsFile() {
Workbook book = null;
try {
book = Workbook.getWorkbook(importFile);//importFile为要导入的xls文件,或二进制的csv文件,提供getXX/setXX方法
} catch (Exception e) {
e.printStackTrace();
}
if(book != null){
int sheetNo = book.getNumberOfSheets();
for(int i = 0; i < sheetNo; i++){
Sheet sheet=book.getSheet(i);
int rowNum = sheet.getRows();
int colNum = sheet.getColumns();
//first row is title, so past
for(int r = 1; r < rowNum; r++){//行
for(int c = 0; c < colNum;c++){//列
String temp = sheet.getCell(c,r).getContents();
switch (c) {
case 0:
if(StringUtils.isNotEmpty(temp)){
linkman.setLinkmanName(temp);
}
break;
case 1:
if(StringUtils.isNotEmpty(temp)){
linkman.setLinkmanEmail(temp);
}
break;
default:
break;
}
}
//保存linkman到数据库
if(linkman.getLinkmanName() != null && linkman.getLinkmanEmail() != null){
EmailLinkmanAPI.insertLinkman(linkman);
}
}
}
}
}
附件为两种导入方法需要用到的jar包