jsp读取txt,excel内容,并显示在jsp求大神解答

yy1230789 2012-09-03 01:38:17
/**
* 读取Excel文件
*
* @param file
* @return
*/
public static List<String[]> readExcel(File file) {
List<String[]> excelValueList = new ArrayList<String[]>();
if (file.exists() && file.canRead()
&& (file.getName().lastIndexOf(".xls")) < 0) {
Workbook workbook = null;// 工作薄对象
try {
workbook = Workbook.getWorkbook(file);
int n = workbook.getNumberOfSheets();
// 循环 行 和 列
for (int i = 0; i < n; i++) {
Sheet sheet = workbook.getSheet(i);
int row = sheet.getRows();// 总行数
int col = sheet.getColumns();// 总列数
for (int r = 0; r < row; r++) {
String[] rowValue = new String[col];
for (int c = 0; c < col; c++) {
rowValue[c] = sheet.getCell(c, r).getContents() != null ? sheet
.getCell(c, r).getContents()
: "";
}
excelValueList.add(rowValue);
}
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (workbook != null) {
workbook.close();
}
}
}
return excelValueList;
}

/**
* 读取 Txt 文件
*
* @param file
* @return
*/
public static List<String> readTxt(File file) throws Exception {
List<String> txtValueList = new ArrayList<String>();
String encoding = "UTF-8";
if (file.exists() && file.canRead()
&& (file.getName().lastIndexOf(".txt") < 0)) {
InputStreamReader read = new InputStreamReader(new FileInputStream(
file), encoding);
BufferedReader buffer = new BufferedReader(read);
String lineTxt;
while ((lineTxt = buffer.readLine()) != null) {
txtValueList.add(lineTxt);
System.out.println(lineTxt);
}
if (read != null) {
read.close();
}
}
return txtValueList;
}
/**
* 读取Excel Txt 文件 UI
*
* @return
*/
public String loadAddTel() {
return "addTel";
}

/**
* 读取Excel Txt文件
*
* @return
* @throws Exception
*/
public String addTel() throws Exception {
if (uploadFileName.lastIndexOf(".xls") >= 1) {
List<String[]> excelValueList = new ArrayList<String[]>();
excelValueList = SendSMSService.readExcel(file);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < excelValueList.size(); i++) {
String s[] = excelValueList.get(i);
for (int n = 0; n < s.length; n++) {
sb.append(s[n]);
sb.append(",");
}
}
tels = sb.toString();
return sendSmsUI();
} else if (uploadFileName.lastIndexOf(".txt") >= 1) {
List<String> txtValueList = new ArrayList<String>();
txtValueList = SendSMSService.readTxt(file);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < txtValueList.size(); i++) {
sb.append(txtValueList.get(i));
sb.append(",");
}
tels = sb.toString();
return sendSmsUI();
} else {
addActionMessage("不允许上传此类文件!");
return "addTel";
}
}

private String sendSmsUI() {
return SUCCESS;
}

...全文
143 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yy1230789 2012-09-03
  • 打赏
  • 举报
回复
么人回答,这我后台已经接到了,但是jsp去不到值