java读取excel、txt 文件内容,传到、显示到另一个页面的文本框里面。

[b]​第一、 utils 工具类,读取Excel 、Txt文件 的方法! 如下:[/b]

[/code]public class Excel {

/**
* 读取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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (workbook != null) {
workbook.close();
}
}
}
return excelValueList;
}

}


public class Txt {


/**
* 读取 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 = null;
while ((lineTxt = buffer.readLine()) != null ) {
txtValueList.add(lineTxt);
}
if (read != null) {
read.close();
}
}
return txtValueList;
}



}

---------------------------------------

[b]
第二,action 调用 。操作 ! 如图:[/b]

[code="java"]

private File upload;
private String uploadFileName;
private String uploadContentType;
private String tels;

/**
* 读取Excel Txt 文件 UI
* @return
*/
public String addExcelUI(){
return "addTel";
}

/**
* 读取Excel Txt文件
* @return
* @throws Exception
*/
public String addTel() throws Exception{
if (uploadFileName.lastIndexOf(".xls") >= 1) {
List<String[]> excelValueList = new ArrayList<String[]>();
excelValueList = Excel.readExcel(upload);
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 = Txt.readTxt(upload);
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";
}
}


第三 ,struts.xml 进行配置,跳转 。 jsp页面调用 如下:
[code="java"][/code]
<textarea name="sms.mobileNumber" οnkeyup="value=this.value.replace(/[^\d,]+/g,'')" id="mobileNumber" style="height: 100px;width: 360px;">${tels}</textarea>


------------------
方法 与 jsp页面 交互的就是 tels 变量 ! 全局的 ~!



***********************************

总结 :这个只点击上传的时候 ,得到里面的数据 。不需要上传到服务器 !
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值