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

来源:互联网 发布:淘宝怎么刷空包 编辑:程序博客网 时间:2024/05/13 14:07

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

 

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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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;}}

 

第二,action 调用 。操作 ! 如图:

           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页面调用 如下:

 

<textarea name="sms.mobileNumber" onkeyup="value=this.value.replace(/[^\d,]+/g,'')" id="mobileNumber" style="height: 100px;width: 360px;">${tels}</textarea> 


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

 

 

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

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

 

 


原创粉丝点击