Java 读取上传文件里的数据,记事本文件和excel文件
本文,文本文件里的数据,每列以 Tab 分隔。
其它分隔符情况下,只需修改对分隔符的判断即可
本文是将文本文件或excel文件里的数据读到List<String[]> 里。
List<Map> , List<ArrayList>, ........可以改为返回其它类型的数据集
UpLoadExcel 类里需要操作excel的 jar包
import org.apache.poi.* ;
网上可以搜索下载,简单快捷
---------------------------------------------------------------------------- - - - public class upLoadAction extends DispatchAction {
public ActionForward doAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
TestForm myForm = (TestForm) form;
FormFile f = myForm.getUpfisle(); //get上传文件
String fileName = f.getFileName();
// 截取文件名的后三位字符
String fileType = fileName.substring(fileName.length()-3,fileName.length());
System.out.println("导入的文件名:"+fileName+"\t 文件后缀名:"+fileType);
List<String[]> list = new ArrayList<String[]>();
if("xls".equals(fileType)){ // 上传文件是excel时文件文件后缀名为xls list = new UpLoadExcel().getExcelData(f.getInputStream());
}else if("txt".equals(fileType)){
list = new UploadText().UploadText(f.getInputStream());
}
// 操作读取出来的数据,例如:
if (list.size() > 0) {
String[] str = null;
for(int i = 0; i < list.size(); i++) {
str = list.get(i);
st = "insert into student (sName,sAge,sAddress,sTelephone) values(" ;
st = "'" + st + str[0] + "'";
st = ",'" + st + str[1] + "'";
st = ",'" + st + str[4] + "'";
st = ",'" + st + str[6] + "'";
st = st + ")";
......
System.out.println(st);
}
}
}
}
-------------------------------------------- - - -- ---------------- -
package myTest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import ermodel.HSSFRow;
import ermodel.HSSFSheet;
import ermodel.HSSFWorkbook;
public class UpLoadExcel {
/**
*要读取的excel文件的数据例如:
*A B C D E F
1112141516
21232526
...
...
*有些行的部分列为空,但其后面的列又有值
*/
public List<String[]> getExcelTest(InputStream is){
// 声明集合 List<String[]> ,
// List<String[]> 的元素行数组String[]为excel中的每一行
List<String[]> list = new ArrayList<String[]>();
try {
// 将is流实例到一个excel流里
HSSFWorkbook hwk = new HSSFWorkbook(is);
// 得到book第一个工作薄sheet
HSSFSheet sh = hwk.getSheetAt(0);
// 总行数
int rows = sh.getLastRowNum()+1 - sh.getFirstRowNum();
// System.out.println(rows);
for(int i=0; i<rows; i++){
HSSFRow row = sh.getRow(i);
int cols = row.getLastCellNum()+1 - row.getFirstCellNum(); // 该行的总列数
String[] str = new String[cols]; // 用来存放该行每一列的值
for (int j = 0; j < cols; j++) {
Object col = row.getCell((short)j);
Object colNext = row.getCell((short)(j+1));
if(col != null){ // 该列不为空,直接读到行数组里
str[j] = col.toString();
}else{ // 该列为空
// 该列的后面一列不为空,用空字符串占位
if(colNext != null){
Object colValue = "";
str[j] = colValue.toString();
}
}
list.add(str);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
---------------------------------------------------------------------------------------------------------------------- - - - -
package myTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class UploadText {
/**
*返回文本每行数据
*/
public List<String[]> UploadText(InputStream inputStream){
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str = null;
List<String[]> list = new ArrayList<String[]>();
try {
while((str = reader.readLine()) != null){
list.add(str.split("\t"));
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}。