- 浏览: 142391 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (119)
- ibatis (7)
- oracle (14)
- struts (4)
- js (11)
- web (6)
- java基础 (16)
- jstl (1)
- use (2)
- log4J (1)
- Json (2)
- jxl (5)
- server (1)
- spring (4)
- jquery (8)
- struts2 (1)
- AjaxAnywhere (1)
- extjs (1)
- hibernate (1)
- other (3)
- tld (1)
- jms (2)
- lib (0)
- 应用 (1)
- web前端 (2)
- linux (2)
- jvm (9)
- 缓存 (1)
- spring mvc (1)
- ftp (1)
- ide (1)
最新评论
1.action的写法
Java代码
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");
}
/**//*
* 从Excel文件中读取数据,并导入到数据库中
**/
public ActionForward getUpload(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 获取excel 文件
Form fm = (Form) actionForm;
FormFile formfile = fm.getUploadfile();
InputStream inputstream = formfile.getInputStream();
fm.clear();// 清空
Session session = HibernateSession.currentSession();
ArrayList list = new ArrayList();
int input = 0; //导入记数
String name = null;
String sex = null;
String age = null;
try {
//通过得到的文件输入流inputstream创建一个HSSFWordbook对象
HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream);
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);//第一个工作表
HSSFRow hssfrow = hssfsheet.getRow(0);//第一行
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++) {
hssfsheet = hssfworkbook.getSheetAt(i);
//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
hssfrow = hssfsheet.getRow(j);
//判断是否还存在需要导入的数据
if (hssfrow == null) {
System.out.println("这里已没有数据,在第"+i+"列,第"+j+"行");
break;
}
/** *//**将EXCEL中的第 j 行,第一列的值插入到实例中*/
if (hssfrow.getCell((short) 0) == null) {
name = "";
} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
name = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
name = hssfrow.getCell((short) 0).getStringCellValue().trim();
}
/** *//**将EXCEL中的第 j 行,第二列的值插入到实例中*/
//姓名
if(hssfrow.getCell((short) 1) == null){
sex = "";
} else if(hssfrow.getCell((short) 1).getCellType() == 0) {
sex = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
sex = hssfrow.getCell((short) 1).getStringCellValue().trim();
}
/** *//**将EXCEL中的第 j 行,第三列的值插入到实例中*/
//姓名
if(hssfrow.getCell((short) 1) == null){
age = "";
} else if(hssfrow.getCell((short) 1).getCellType() == 0) {
age = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
age = hssfrow.getCell((short) 1).getStringCellValue().trim();
}
name = name.trim();
sex = sex.toUpperCase();
if (name.equals("")) {
error.setName(name);
error.setMessage("姓名不能为空");
list.add(error);
continue;
} else {
fm.setName(name);
fm.setSex(sex);
fm.setAge(age);
session.save(fm);
}
//导入成功加1
input++;
}
}
session.saveObjs(list.toArray());
} catch () {
}
}
}
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");
}
/**//*
* 从Excel文件中读取数据,并导入到数据库中
**/
public ActionForward getUpload(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 获取excel 文件
Form fm = (Form) actionForm;
FormFile formfile = fm.getUploadfile();
InputStream inputstream = formfile.getInputStream();
fm.clear();// 清空
Session session = HibernateSession.currentSession();
ArrayList list = new ArrayList();
int input = 0; //导入记数
String name = null;
String sex = null;
String age = null;
try {
//通过得到的文件输入流inputstream创建一个HSSFWordbook对象
HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream);
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);//第一个工作表
HSSFRow hssfrow = hssfsheet.getRow(0);//第一行
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++) {
hssfsheet = hssfworkbook.getSheetAt(i);
//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
hssfrow = hssfsheet.getRow(j);
//判断是否还存在需要导入的数据
if (hssfrow == null) {
System.out.println("这里已没有数据,在第"+i+"列,第"+j+"行");
break;
}
/** *//**将EXCEL中的第 j 行,第一列的值插入到实例中*/
if (hssfrow.getCell((short) 0) == null) {
name = "";
} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
name = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
name = hssfrow.getCell((short) 0).getStringCellValue().trim();
}
/** *//**将EXCEL中的第 j 行,第二列的值插入到实例中*/
//姓名
if(hssfrow.getCell((short) 1) == null){
sex = "";
} else if(hssfrow.getCell((short) 1).getCellType() == 0) {
sex = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
sex = hssfrow.getCell((short) 1).getStringCellValue().trim();
}
/** *//**将EXCEL中的第 j 行,第三列的值插入到实例中*/
//姓名
if(hssfrow.getCell((short) 1) == null){
age = "";
} else if(hssfrow.getCell((short) 1).getCellType() == 0) {
age = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
age = hssfrow.getCell((short) 1).getStringCellValue().trim();
}
name = name.trim();
sex = sex.toUpperCase();
if (name.equals("")) {
error.setName(name);
error.setMessage("姓名不能为空");
list.add(error);
continue;
} else {
fm.setName(name);
fm.setSex(sex);
fm.setAge(age);
session.save(fm);
}
//导入成功加1
input++;
}
}
session.saveObjs(list.toArray());
} catch () {
}
}
}
2.Form的写法
Java代码
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class Form extends ActionForm {
// 上传的文件
private FormFile _flddo;
public void setUploadfile(FormFile formfile) {
_flddo = formfile;
}
public FormFile getUploadfile() {
return _flddo;
}
public void clear() {
_flddo = null;
}
}
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class Form extends ActionForm {
// 上传的文件
private FormFile _flddo;
public void setUploadfile(FormFile formfile) {
_flddo = formfile;
}
public FormFile getUploadfile() {
return _flddo;
}
public void clear() {
_flddo = null;
}
}
3.上传页面Upload.jsp
Java代码
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<html:form action="/Action.do?method=getUpload" method="POST" enctype="multipart/form-data">
<html:file property="uploadfile" size="80%" />
<input type="button" value="导 入" onclick="upload(this.form)" class="buttonGray">
</html:form>
</html>
<script language="javascript">
function upload(obj)
{
if(confirm("您现在选择的是XXX,您确定要导入吗?"))
{
var uploadfile = document.all.uploadfile.value;
if((null == uploadfile) ||( "" == uploadfile))
{
alert("上传文件没有指定!");
return false;
}
obj.action = '<html:rewrite page="/Action.do?method=getUpload"/>';
obj.submit();
}
}
</script>
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<html:form action="/Action.do?method=getUpload" method="POST" enctype="multipart/form-data">
<html:file property="uploadfile" size="80%" />
<input type="button" value="导 入" onclick="upload(this.form)" class="buttonGray">
</html:form>
</html>
<script language="javascript">
function upload(obj)
{
if(confirm("您现在选择的是XXX,您确定要导入吗?"))
{
var uploadfile = document.all.uploadfile.value;
if((null == uploadfile) ||( "" == uploadfile))
{
alert("上传文件没有指定!");
return false;
}
obj.action = '<html:rewrite page="/Action.do?method=getUpload"/>';
obj.submit();
}
}
</script>
4.下载页面Download.jsp
Java代码
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%
//获取下载文件
String download = (String) request.getSession().getAttribute("Download");
//清空文件
request.getSession().removeAttribute("Download");
%>
<html>
下传文件 <a href="<%=download %>" name="下载">下载</a>
</html>
Java代码
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");
}
/**//*
* 从Excel文件中读取数据,并导入到数据库中
**/
public ActionForward getUpload(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 获取excel 文件
Form fm = (Form) actionForm;
FormFile formfile = fm.getUploadfile();
InputStream inputstream = formfile.getInputStream();
fm.clear();// 清空
Session session = HibernateSession.currentSession();
ArrayList list = new ArrayList();
int input = 0; //导入记数
String name = null;
String sex = null;
String age = null;
try {
//通过得到的文件输入流inputstream创建一个HSSFWordbook对象
HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream);
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);//第一个工作表
HSSFRow hssfrow = hssfsheet.getRow(0);//第一行
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++) {
hssfsheet = hssfworkbook.getSheetAt(i);
//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
hssfrow = hssfsheet.getRow(j);
//判断是否还存在需要导入的数据
if (hssfrow == null) {
System.out.println("这里已没有数据,在第"+i+"列,第"+j+"行");
break;
}
/** *//**将EXCEL中的第 j 行,第一列的值插入到实例中*/
if (hssfrow.getCell((short) 0) == null) {
name = "";
} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
name = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
name = hssfrow.getCell((short) 0).getStringCellValue().trim();
}
/** *//**将EXCEL中的第 j 行,第二列的值插入到实例中*/
//姓名
if(hssfrow.getCell((short) 1) == null){
sex = "";
} else if(hssfrow.getCell((short) 1).getCellType() == 0) {
sex = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
sex = hssfrow.getCell((short) 1).getStringCellValue().trim();
}
/** *//**将EXCEL中的第 j 行,第三列的值插入到实例中*/
//姓名
if(hssfrow.getCell((short) 1) == null){
age = "";
} else if(hssfrow.getCell((short) 1).getCellType() == 0) {
age = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
age = hssfrow.getCell((short) 1).getStringCellValue().trim();
}
name = name.trim();
sex = sex.toUpperCase();
if (name.equals("")) {
error.setName(name);
error.setMessage("姓名不能为空");
list.add(error);
continue;
} else {
fm.setName(name);
fm.setSex(sex);
fm.setAge(age);
session.save(fm);
}
//导入成功加1
input++;
}
}
session.saveObjs(list.toArray());
} catch () {
}
}
}
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");
}
/**//*
* 从Excel文件中读取数据,并导入到数据库中
**/
public ActionForward getUpload(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 获取excel 文件
Form fm = (Form) actionForm;
FormFile formfile = fm.getUploadfile();
InputStream inputstream = formfile.getInputStream();
fm.clear();// 清空
Session session = HibernateSession.currentSession();
ArrayList list = new ArrayList();
int input = 0; //导入记数
String name = null;
String sex = null;
String age = null;
try {
//通过得到的文件输入流inputstream创建一个HSSFWordbook对象
HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream);
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);//第一个工作表
HSSFRow hssfrow = hssfsheet.getRow(0);//第一行
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++) {
hssfsheet = hssfworkbook.getSheetAt(i);
//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
hssfrow = hssfsheet.getRow(j);
//判断是否还存在需要导入的数据
if (hssfrow == null) {
System.out.println("这里已没有数据,在第"+i+"列,第"+j+"行");
break;
}
/** *//**将EXCEL中的第 j 行,第一列的值插入到实例中*/
if (hssfrow.getCell((short) 0) == null) {
name = "";
} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
name = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
name = hssfrow.getCell((short) 0).getStringCellValue().trim();
}
/** *//**将EXCEL中的第 j 行,第二列的值插入到实例中*/
//姓名
if(hssfrow.getCell((short) 1) == null){
sex = "";
} else if(hssfrow.getCell((short) 1).getCellType() == 0) {
sex = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
sex = hssfrow.getCell((short) 1).getStringCellValue().trim();
}
/** *//**将EXCEL中的第 j 行,第三列的值插入到实例中*/
//姓名
if(hssfrow.getCell((short) 1) == null){
age = "";
} else if(hssfrow.getCell((short) 1).getCellType() == 0) {
age = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
}
//如果EXCEL表格中的数据类型为字符串型
else {
age = hssfrow.getCell((short) 1).getStringCellValue().trim();
}
name = name.trim();
sex = sex.toUpperCase();
if (name.equals("")) {
error.setName(name);
error.setMessage("姓名不能为空");
list.add(error);
continue;
} else {
fm.setName(name);
fm.setSex(sex);
fm.setAge(age);
session.save(fm);
}
//导入成功加1
input++;
}
}
session.saveObjs(list.toArray());
} catch () {
}
}
}
2.Form的写法
Java代码
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class Form extends ActionForm {
// 上传的文件
private FormFile _flddo;
public void setUploadfile(FormFile formfile) {
_flddo = formfile;
}
public FormFile getUploadfile() {
return _flddo;
}
public void clear() {
_flddo = null;
}
}
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class Form extends ActionForm {
// 上传的文件
private FormFile _flddo;
public void setUploadfile(FormFile formfile) {
_flddo = formfile;
}
public FormFile getUploadfile() {
return _flddo;
}
public void clear() {
_flddo = null;
}
}
3.上传页面Upload.jsp
Java代码
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<html:form action="/Action.do?method=getUpload" method="POST" enctype="multipart/form-data">
<html:file property="uploadfile" size="80%" />
<input type="button" value="导 入" onclick="upload(this.form)" class="buttonGray">
</html:form>
</html>
<script language="javascript">
function upload(obj)
{
if(confirm("您现在选择的是XXX,您确定要导入吗?"))
{
var uploadfile = document.all.uploadfile.value;
if((null == uploadfile) ||( "" == uploadfile))
{
alert("上传文件没有指定!");
return false;
}
obj.action = '<html:rewrite page="/Action.do?method=getUpload"/>';
obj.submit();
}
}
</script>
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<html:form action="/Action.do?method=getUpload" method="POST" enctype="multipart/form-data">
<html:file property="uploadfile" size="80%" />
<input type="button" value="导 入" onclick="upload(this.form)" class="buttonGray">
</html:form>
</html>
<script language="javascript">
function upload(obj)
{
if(confirm("您现在选择的是XXX,您确定要导入吗?"))
{
var uploadfile = document.all.uploadfile.value;
if((null == uploadfile) ||( "" == uploadfile))
{
alert("上传文件没有指定!");
return false;
}
obj.action = '<html:rewrite page="/Action.do?method=getUpload"/>';
obj.submit();
}
}
</script>
4.下载页面Download.jsp
Java代码
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%
//获取下载文件
String download = (String) request.getSession().getAttribute("Download");
//清空文件
request.getSession().removeAttribute("Download");
%>
<html>
下传文件 <a href="<%=download %>" name="下载">下载</a>
</html>
相关推荐
web中使用POI导入导出EXCEL文件的例子 struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet....
程序使用Struts2(SSH即Struts2+Hibernate+Spring)+POI+MySQL实现Excel动态数据导入和导出,程序源码文件附带运行说明
1.本动态导入导出Excel工程导入Eclipse即可用行,支持所有版本的Excel导入导出。 2.程序使用Struts2+POI+SQLSever(SSH即Struts2+Hibernate+Spring)实现Excel动态数据导入和导出,压缩包内含程序源码文件。
struts2+poi实现excel文件的导出,虽然有很多jxl库实现的方案,而本方案是使用struts2 mvc, apache poi库实现excel的导入导出,文档很详细
运用struts2将数据库中的数据导入和导出,同时会用到poi的jar包
poi即Excel导入导出,在开发过程中了,为了提高数据录入速度, 一些系统都会使用导入功能,导入目前只能通过流的方式读写,那么为了此操作的便利性,提供此工具类,是原有的excel数据读取到泛型集合中,然后再对集合...
jsp通过POI将数据从数据库导出至EXCEL,可与CSDN中资源 【jsp中使用poi导入导出Excel http://download.csdn.net/detail/guan_xiu/5978843】对比学习; 前者是使用最原始的方法jsp+jdbc+poi; 后者加入了struts处理...
自己写一个可直接导出List<Object>数据的Excel工具类,使用起来很方便。
程序使用Struts2(SSH即Struts2+Hibernate+Spring)+POI+MySQL实现Excel动态数据导入和导出,程序源码文件附带运行说明
程序使用Struts2(SSH即Struts2+Hibernate+Spring)+POI+MySQL实现Excel动态数据导入和导出,程序源码文件附带运行说明
使用Spring集成struts2、ibatis、poi实现的增删改查功能,包括采用jquery实现的无刷新查询机分页、dwr实现的两级联动、以及采用poi动态将数据库数据导出成excel,本demo采用mysql数据库,附有建表sql,项目导入...
9、用POI实现对数据的导入导出功能,及POI对excel的操作。 10、后台权限采用流行的shiro权限管理框架,通过本项目可以深入了解shiro权限框架的应用及原理。 11、项目部署采用tomcat+Nginx的集群部署方式,在...
2、后台:Struts2、动态代理、Mybatis框架、log4J日志框架、jstl 、jstl自定义分页标签、代码机器人、Dwr、POI(实现Excel文件的导入导出)等 3、数据库:Mysql 4、服务器:Tomcat 项目中涉及的功能: 1、项目以及...
采用技术: SSH2(Struts2 + Spring + Hibernate),标准SSH2框架,采用了基于注解方式管理bean和事务,使用poi进行excel批量导出 图书类型:图书类别,类别名称,可借阅天数 图书:图书条形码,图书名称,图书所在...