欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据导出

程序员文章站 2022-05-13 17:44:09
...
package com;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


	public class PermitAction extends DispatchAction
	{
		public ActionForward getPermitList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
		{
			String fuserId = request.getParameter("fuserId");
		
			try
			{
				response.getWriter().print("<script>history.back();alert('hjhjhjhjhjh');</script>");
				------------直接返回到当前页面----------
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			return null;
		}
	}

 

 

    数据导出:

	// 导出Excel
        // 先将数据导到服务器上的路径,然后下载
String path = request.getSession().getServletContext().getRealPath("excel") + File.separator + "locationresult.xls";
// 先将数据导到服务器上的路径:exportExcel 函数如下
this.exportExcel(sql.toString(), path);

File file = new File(path);
// application/vnd.ms-excel : 这个可去tomcat 配置文件(web.xml)中找  还有其他类型
response.setContentType("application/vnd.ms-excel");

response.setHeader("Content-Disposition", "attachment; filename=locationresult.xls");
response.setContentType("application/x-msdownload");
InputStream is = new FileInputStream(file);
OutputStream os = response.getOutputStream();
int i = 0;
byte[] buffer = new byte[10240];
while ((i = is.read(buffer, 0, buffer.length)) > -1)
{
	os.write(buffer, 0, i);
	os.flush();
}
if (is != null)
{
 is.close();
}
if (os != null)
{
	os.close();
}

==================
public void exportExcel(String sql, String path)
	{
		ResultSet rs = null;
		Statement stmt = null;
		Connection conn =  null;
		try
		{
			WritableWorkbook book = Workbook.createWorkbook(new File(path));
			// 生成名为"第一页"的工作表,参数0表示这是第一页
			WritableSheet sheet = book.createSheet("第u19968 页, 0);
			// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0) //以及单元格内容为test
			 conn = oracleToolIn.getConnection();//获取数据方法自己修改
			 stmt = conn.createStatement();
			 rs = stmt.executeQuery(sql);
			ResultSetMetaData rsmd = rs.getMetaData();
			int colCnt = rsmd.getColumnCount();
			for (int k = 1; k < colCnt + 1; k++)
			{
				String title = rsmd.getColumnName(k);
				Label labelTitle = new Label(k - 1, 0, title);
				sheet.addCell(labelTitle);
			}

			int i = 1;
			while (rs.next())
			{
				for (int j = 1; j < colCnt + 1; j++)
				{
					String colName = rsmd.getColumnName(j);
					String colValue = rs.getString(colName);
					Label label = new Label(j - 1, i, colValue);
					sheet.addCell(label);
				}
				i++;
			}
			
			book.write();
			book.close();
		}
		catch (Exception e)
		{
			
		}
		finally
		{
			
			try 
			{
				if(rs!=null)
				{
						rs.close();
				}
				if(stmt!=null)
				{
					stmt.close();
				}
				if(conn!=null)
				{
					conn.close();
				}
				}catch (Exception e) 
					{
						Log4jInit.logger().error(e.toString());
					}
			}
	} 

=================================
// 导出HTML

// 先将数据导到服务器上的路径,然后下载
				String path = request.getSession().getServletContext().getRealPath("excel") + File.separator + "motesss.html";
				this.exportHtml(sql.toString(), path);
				response.setContentType("text/html");
				response.setHeader("Content-Disposition", "attachment; filename=mouserlist.html");
				response.setContentType("application/x-download");
				InputStream is = new FileInputStream(path);
				OutputStream os = response.getOutputStream();
				int i = 0;
				byte[] buffer = new byte[10240];
				while ((i = is.read(buffer, 0, buffer.length)) > -1)
				{
					os.write(buffer, 0, i);
					os.flush();
				}
				if (is != null)
				{
					is.close();
				}
				if (os != null)
				{
					os.close();
				}
==================
public void exportHtml(String sql, String path)
	{
		PrintWriter pw = null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try
		{
			 conn = oracleToolIn.getConnection();
			 stmt = conn.createStatement();
			 rs = stmt.executeQuery(sql);
			ResultSetMetaData rsmd = rs.getMetaData();
			StringBuffer html = new StringBuffer("<table border=1>");
			StringBuffer title = new StringBuffer("<tr>");
			int colCnt = rsmd.getColumnCount();
			for (int k = 1; k < colCnt + 1; k++)
			{
				title.append("<td>").append(rsmd.getColumnName(k)).append("</td>");
			}
			title.append("</tr>");
			html.append(title.toString());
			while (rs.next())
			{
				html.append("<tr>");
				for (int j = 1; j < colCnt + 1; j++)
				{
					String colName = rsmd.getColumnName(j);
					String colValue = rs.getString(colName);
					html.append("<td>").append(colValue).append("</td>");
				}
				html.append("</tr>");
			}
		
			html.append("</table>");
			File f =  new File(path);
			pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f),"gb2312"));
			pw.write(html.toString());
			pw.flush();
			pw.close();
		}
		catch (Exception e)
		{
			Log4jInit.logger().error(e.toString());
		}
		finally
		{
			
			try 
			{
				if(rs!=null)
				{
					rs.close();
				}
				if(stmt!=null)
				{
					stmt.close();
				}
				if(conn!=null)
				{
					conn.close();
				}
				}catch (Exception e) 
					{
						Log4jInit.logger().error(e.toString());
					}
			}

	}