87,991
社区成员
发帖
与我相关
我的任务
分享/**
* 读取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;
}