struts2-excel文件的读取

页面:
<input type="file" id="filePath" name="filePath" value="${filePath?if_exists}" title="${action.getText('operator.excelpath')}" class="textbox_css" style="width:500px" />

<input type="button" class="btn" name="btnUpload" value=" ${action.getText("system.board.upload")} " οnclick="addOperatorUpload();" />

//文件上传操作
function addOperatorUpload(){
/**上传文件不能用href,不然后台myFile拿不到值*/
var filepath=document.getElementById("filePath").value;
if(''==filepath){
alert("请选择要上传的excle文件");
return;
}
document.location.href="${base}/system/operatorBatchManager.action?act=doOther&filePath="+filepath;
}

********************************************************************
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;
action:
private String filePath;

/**
* 文件
*/
private File myFile;

// myFileContentType属性用来封装上传文件的类型
private String myFileContentType;

// myFileFileName属性用来封装上传文件的文件名
private String myFileFileName;
//以上get/set方法略
/**
* excel文件读取
*/
public String doOther() throws Exception {

myFile = new File(filePath);
// 获得文件名
String type = myFile.getName();// this.getMyFileFileName();
int i = 0;
while (i != -1) {
// 找到上传文件的类型的位置,这个地方的是'.'
i = type.indexOf(".");
// 截取上传文件的后缀名,此时得到了文件的类型
type = type.substring(i + 1);
}
// 如果是excle文件就开始处理
if ("xls".equals(type) || "xlsx".equals(type)) {
// 获得文件路径
String str_path = myFile.toString();
this.excelDataImportOracle(str_path);
} else {
// 文件类型不对
this.setErrorInfo(getText("info.fileErrorType"));
}
return "addInput";
}


public void excelDataImportOracle(String pathname) throws Exception {
try {
ArrayList<AdminWorkArea> al = this.readExcel(pathname);
// 将数据保存给分页对象返回页面
this.pageInfo.setSearchResult(al);
}


public ArrayList<AdminWorkArea> readExcel(String pathname) {
String cellStr = null;// 单元格,最终按字符串处理
InputStream is = null;
ArrayList<AdminWorkArea> list = new ArrayList<AdminWorkArea>();
try {
is = new FileInputStream(new File(pathname));// 获取文件输入流
HSSFWorkbook workbook = new HSSFWorkbook(is);// 创建Excel2003文件对象
HSSFSheet sheet = workbook.getSheetAt(0);// 取出第一个工作表,索引是0
// 开始循环遍历行,表头不处理,从1开始
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
HSSFRow row = sheet.getRow(i);// 获取行对象
if (row == null) {// 如果为空,不处理
continue;
}

AdminWorkArea o = new AdminWorkArea();

// 循环遍历单元格
for (int j = 0; j < row.getLastCellNum(); j++) {
HSSFCell cell = row.getCell((short) j);// 获取单元格对象
if (cell == null) {// 单元格为空设置cellStr为空串
cellStr = "";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理
cellStr = String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理
cellStr = String.valueOf(cell.getNumericCellValue());
} else {// 其余按照字符串处理
cellStr = cell.getStringCellValue();
}
// 下面按照数据出现位置封装到bean中
switch (j) {
case 0: o.setLogin(cellStr);
break;
case 1: o.setName(cellStr);
break;
case 2: o.setAgentSkills(cellStr);
break;
case 4: o.setSubccNo(cellStr);
break;
case 5: o.setVdn(cellStr);
break;
case 6:// 工作组id
o.setGroupId(cellStr);
break;
}
}
// 登录名做为键,保存到map中到时用于增加
map.put(o.getLogin(), o);
list.add(o);// 将excel用户列表放入集合中
}
} catch (Exception e) {
e.printStackTrace();
} finally {// 关闭文件流
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}

************************************************************
struts.xml文件:
<!-- 批量用户管理-->
<action name="operatorBatchManager" class="com.tempus.fw.operatorBatchAction">
<result name="addInput" type="freemarker">
/WEB-INF/core/pages/system/operatorBatchOpt.ftl
</result>
</action>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
web中使用POI导入导出EXCEL文件的例子 struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.*; import org.apache.struts.action.*; import org.apache.struts.upload.FormFile; import org.apache.commons.beanutils.BeanUtils; public class Action { /**//* * 把数据库中的字段导入到Excel ,并生成Excel文档 **/ public ActionForward getDownload(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { Form fm = (Form) actionForm; // Excel 文件存放在服务器的相对路径下 String outputFile = request.getRealPath("/tmp/Excel.xls"); try { // 创建新的Excel 工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 在Excel 工作簿中建一工作表 HSSFSheet sheet = workbook.createSheet("Sheet1"); // 设置单元格格式(文本) HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@")); // 在索引0的位置创建行(第一行) HSSFRow row = sheet.createRow((short) 0); HSSFCell cell1 = row.createCell((short) 0);// 第一列 HSSFCell cell2 = row.createCell((short) 1); HSSFCell cell3 = row.createCell((short) 2); // 定义单元格为字符串类型 cell1.setCellType(HSSFCell.CELL_TYPE_STRING); cell2.setCellType(HSSFCell.CELL_TYPE_STRING); cell3.setCellType(HSSFCell.CELL_TYPE_STRING); cell1.setEncoding(HSSFCell.ENCODING_UTF_16); cell2.setEncoding(HSSFCell.ENCODING_UTF_16); cell3.setEncoding(HSSFCell.ENCODING_UTF_16); // 在单元格中输入数据 cell1.setCellValue("姓名"); cell2.setCellValue("性别"); cell3.setCellValue("年龄"); Connection connection = session.connection(); String sql = "Select t.name, t.sex, t.age from table t where t.sex = ?"; try { PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, fm.getSex());// 传入查询条件 ResultSet rs = ps.executeQuery();// 查询结果存入rs connection.commit();// 执行SQL while (rs.next()) { //设置j行从第二行开始 int j = 1; row = sheet.createRow((short) j); //设置i列从第二列开始 for (int i = 1; i <= 3; i++) { HSSFCell cell = row.createCell((short) (i-1)); // 设置单元格格式 cell.setCellStyle(cellStyle); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(rs.getString(i)); } j++; } request.setAttribute("message", "文件生成成功!"); } catch (SQLException e) { request.setAttribute("message", "创建文件失败!"); e.printStackTrace(); } // 删除路径下同名的Excel 文件 File path = new File(outputFile); path.delete(); // 新建一输出文件流 FileOutputStream fOut = new FileOutputStream(outputFile); // 把相应的Excel 工作簿存盘 workbook.write(fOut); // 操作结束,关闭文件 fOut.flush(); fOut.close(); //该处如果Excel过大会影响效率,谁有好的想法可以提出来参考(不过从页面下载完后就会清空) request.getSession().setAttribute("Download", outputFile); } catch (Exception ioexception) { request.setAttribute("message", "创建文件失败!"); return actionMapping.findForward("outJSP"); } return actionMapping.findForward("outJSP"); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值