一、上传
引入包:org.apache.commons.fileupload.*
jsp代码: DiskFileUpload fu = new DiskFileUpload();
List list = fu.parseRequest(request);
if(list != null){
System.out.println(list.size());
String [] nsrbms = null;
for (int i = 0;i < list.size(); i++){
FileItem fi = (FileItem)list.get(i);
if(fi.isFormField()) //这是用来确定是否为文件属性,
{
String fieldName = fi.getFieldName(); //这里取得表单名
String fieldvalue=fi.getString(); //这里取得表单值
if(fieldName.equals("sbqlx")){
searchSbqlx = fieldvalue;
}else if(fieldName.equals("ruleId")){
searchRuleId = fieldvalue;
}else if(fieldName.equals("qylx")){
searchQylx = fieldvalue;
}else if(fieldName.equals("sssqQ")){
searchSssqQ = fieldvalue;
}else if(fieldName.equals("sssqZ")){
searchSssqZ = fieldvalue;
}
}
else{//如果 是文件
if(fi.getSize() != 0){
nsrbms = CollectUtil.getNsrbmFromFile(fi.getInputStream());
}
}
}
二、读取excel文件:
引入包:import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
主要方法:
/**
* 获取某一单元格的值
* @param cell
* @return
*/
private static String getCellStringValue(HSSFCell cell)
{
String value = "";
if (cell == null) {
return value;
}
//按字符串取值
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
value = cell.getStringCellValue();
}
//按数字取值
if (cell.getCellType()
== HSSFCell.CELL_TYPE_NUMERIC) {
DecimalFormat formatter =
new DecimalFormat("############"); //对数字格式化
value = String.valueOf(
formatter.format(cell.getNumericCellValue()));
}
//空值
if (cell.getCellType()
== HSSFCell.CELL_TYPE_BLANK) {
value = "";
}
//非法字符
if (cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
value = "";
}
return value.trim();
}
/**
* 读取io文件流,返回纳税人编码数组
*/
public static String[] getNsrbmFromFile(InputStream inputStream){
String[] nsrbms = null;
HSSFWorkbook hwb = null;
try {
hwb = new HSSFWorkbook(inputStream);//通过输入流构建excel读取对象
HSSFSheet sheet = hwb.getSheetAt(0);//获取excel中的第一个sheet
int last = sheet.getLastRowNum();//获取excel中记录的行数
nsrbms = new String[last];
for(int i = 1; i <= last ;i ++){//从第二行开始读取数据
HSSFRow row = sheet.getRow(i);
HSSFCell cell = row.getCell(Short.valueOf("0").shortValue());//获取当前行的第1列
nsrbms[i-1] = getCellStringValue(cell);
System.out.println(getCellStringValue(cell));
}
} catch (IOException e) {
e.printStackTrace();
}
return nsrbms;
}