1.在index.xml编写代码
<form action="importExcel" method="post"enctype="multipart/form-data">
<input name="file" type="file">
<input type="submit" value="导入"/>
</form>
2.在struts.xml配置
<struts>
<package name="team" extends="struts-default" namespace="/">
<!--导入-->
<action name="importExcel" class="com.iteye.action.TestAction" method="importExcel">
<result>/index.jsp</result>
</action>
</package>
</struts>
3.在Action完成Excel导入
public class TestAction extends ActionSupport{
private File file;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
/**
* excel导入
* @throws IOException
* @throws FileNotFoundException
*/
public String excelImport() throws FileNotFoundException, IOException{
ImageDao dao = new ImageDao();
//工作簿
HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(file));
//获取工作簿的单元
HSSFSheet sheet =book.getSheetAt(0);
//循环获取行后在获取对应的列
int i = 0;
while(true){
HSSFRow row = sheet.getRow(i);
if(row==null) break;
HSSFCell cellold = row.getCell(0);
HSSFCell cellnew = row.getCell(1);
HSSFCell cellpath = row.getCell(2);
TImage t = new TImage();
t.setOldname(cellold.toString());
t.setNewname(cellnew.toString());
t.setPathimage(cellpath.toString());
dao.saveUser(t);
i++;
}
return SUCCESS;
}
}