经验代码共享:导出到Excel/Word/Txt

zyhowe 2009-06-06 05:46:59
来源:http://www.zyhowe.com/index/topicDetail.aspx?type=200300&topicid=283

/**
* 文件导出
*/

// import java.io.*;
// import javax.servlet.*;
import javax.servlet.http.*;
import java.util.List;
import java.util.Iterator;
import com.finchina.fund.data.*;

public class ExportUtil {

/**
* TXT导出文件
*
* @param type
* @param title
* @param str
* @param footStr
* @param response
* @param out
* @param fileName
*/
public static void OutFile(String type, String title, String str,
String footStr, HttpServletResponse response,
javax.servlet.jsp.JspWriter out, String fileName) {
response.resetBuffer();

String contentType = "";
if (type.equals("doc"))
contentType = "application/msword";
else if (type.equals("xls"))
contentType = "application/vnd.ms-excel";
else if (type.equals("txt"))
contentType = "text/plain";
else
contentType = "application/msword";// doc,contentType =
// "application/octet-stream";

response.setContentType(contentType + ";charset=GBK");
response.setHeader("Content-Disposition", "attachment;filename="
+ Normal.ciso(fileName));
try {
if (type.equals("xls")) {
out.println(GetHeader().toString());
out.println(GetBodyXls(title, str).toString());
} else if (type.equals("txt")) {
out.println(GetBodyTxt(title, str).toString());
} else {
out.println(GetHeader().toString());
out.println(GetBodyDoc(title, str).toString());
}
out.println(GetFoot(footStr).toString());
out.flush();
out.close();

response.flushBuffer();
} catch (Exception e) {
}

}

/**
* SQL导出文件
*
* @param type
* @param title
* @param str
* @param footStr
* @param response
* @param out
* @param fileName
*/
public static void OutFileSql(String type, String title, String str,
String footStr, HttpServletResponse response,
javax.servlet.jsp.JspWriter out, String fileName) {
response.resetBuffer();

String contentType = "";
if (type.equals("doc"))
contentType = "application/msword";
else if (type.equals("xls"))
contentType = "application/vnd.ms-excel";
else if (type.equals("txt"))
contentType = "text/plain";
else
contentType = "application/msword";// doc,contentType =
// "application/octet-stream";

response.setContentType(contentType + ";charset=GBK");
response.setHeader("Content-Disposition", "attachment;filename="
+ Normal.ciso(fileName));
try {
if (type.equals("xls")) {
out.println(GetHeader().toString());
out.println(GetBodyXlsSql(title, str).toString());
} else if (type.equals("txt")) {
out.println(GetBodyTxtSql(title, str).toString());
} else {
out.println(GetHeader().toString());
out.println(GetBodyDocSql(title, str).toString());
}
out.println(GetFoot(footStr).toString());
out.flush();
out.close();

response.flushBuffer();
} catch (Exception e) {
}

}

/**
* TXT导出EXCEL
*
* @param title
* @param str
* @param footStr
* @param response
* @param out
* @param fileName
*/
public static void OutExcel(String title, String str, String footStr,
HttpServletResponse response, javax.servlet.jsp.JspWriter out,
String fileName) {
OutFile("xls", title, str, footStr, response, out, fileName);
}

/**
* SQL导出EXCEL
*
* @param title
* @param str
* @param footStr
* @param response
* @param out
* @param fileName
*/
public static void OutExcelSql(String title, String str, String footStr,
HttpServletResponse response, javax.servlet.jsp.JspWriter out,
String fileName) {
OutFileSql("xls", title, str, footStr, response, out, fileName);
}

/**
* 字符导出TXT
*
* @param title
* @param str
* @param footStr
* @param response
* @param out
* @param fileName
*/
public static void OutTxt(String title, String str, String footStr,
HttpServletResponse response, javax.servlet.jsp.JspWriter out,
String fileName) {
OutFile("txt", title, str, footStr, response, out, fileName);
}

/**
* SQL导出TXT
*
* @param title
* @param str
* @param footStr
* @param response
* @param out
* @param fileName
*/
public static void OutTxtSql(String title, String str, String footStr,
HttpServletResponse response, javax.servlet.jsp.JspWriter out,
String fileName) {
OutFileSql("txt", title, str, footStr, response, out, fileName);
}

/**
* 字符导出WORD
*
* @param title
* @param str
* @param footStr
* @param response
* @param out
* @param fileName
*/
public static void OutWord(String title, String str, String footStr,
HttpServletResponse response, javax.servlet.jsp.JspWriter out,
String fileName) {
OutFile("doc", title, str, footStr, response, out, fileName);
}

/**
* SQL导出WORD
*
* @param title
* @param str
* @param footStr
* @param response
* @param out
* @param fileName
*/
public static void OutWordSql(String title, String str, String footStr,
HttpServletResponse response, javax.servlet.jsp.JspWriter out,
String fileName) {
OutFileSql("doc", title, str, footStr, response, out, fileName);
}

/**
* Excel样式加载
*
* @return
*/
public static StringBuffer GetHeader() {
StringBuffer sb = new StringBuffer();
sb.append("<head>\n\r");

sb
.append("<meta http-equiv=Content-Type content=\"text/html; charset=gbk\">\n\r");
sb.append("<style>\n\r");
sb.append("td {font-size:12px;border:.5pt solid black;}\n\r");
sb
.append(".title {font-size:14px; font-weight:bold;height:30px;border:0px}\n\r");
sb.append(".title1 {font-size:14px; height:30px;border:0px}\n\r");
sb.append(".thead{font-weight:bold;}\n\r");
sb
.append(".style0{mso-number-format:General;text-align:general;vertical-align:middle;white-space:normal;"
+ "mso-rotate:0;mso-background-source:auto;mso-pattern:auto;color:windowtext;"
+ "font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;"
+ "mso-generic-font-family:auto;mso-font-charset:134;border:none;"
+ "mso-protection:locked visible;mso-style-name:常规;mso-style-id:0;"
+ "font-size:9.0pt;border:.5pt solid black;}\n\r");
sb
.append(".x1281{mso-style-parent:style0;mso-number-format:\"\\@\";border:.5pt solid black;font-weight:bold;}\n\r");
sb
.append(".x1282{mso-style-parent:style0;mso-number-format:\"\\@\";border:.5pt solid black;}\n\r");
sb
.append(".x0{mso-style-parent:style0;mso-number-format:\"0_ \";text-align:right;border:.5pt solid black;}\n\r");
sb
.append(".x1{mso-style-parent:style0;mso-number-format:\"0\\.0_ \";text-align:right;border:.5pt solid black;}\n\r");
sb
sb.append("</style>\n\r");
sb.append("</head>\n\r");
sb.append("<body>\n\r");

return sb;
}

/**
* 字符导出Excel
*
* @param title
* @param str
* @return
*/
public static StringBuffer GetBodyXls(String title, String str) {
StringBuffer s = new StringBuffer();
s
.append("<table ID=\"Table0\" BORDER=0 CELLSPACING=1 CELLPADDING=3 width=100% align=center>\n\r");
s.append("<tr>");
s.append("<td align=center class=\"title\" colspan=\"10\">" + title
+ "</td>");
s.append("</tr>\n\r");
s.append("</table>\n\r");
s.append(str);
return s;
}

/**
* SQL导出EXCEL
*
* @param title
* @param sql
* @return
*/
public static StringBuffer GetBodyXlsSql(String title, String sql) {
StringBuffer s = new StringBuffer();
s
.append("<table ID=\"Table0\" BORDER=0 CELLSPACING=1 CELLPADDING=3 width=100% align=center>\n\r");
s.append("<tr>");
s.append("<td colspan=\"10\" align=center class=\"title\">" + title
+ "</td>");
s.append("</tr>\n\r");
s.append("</table>\n\r");
s
.append("<table border=0 cellspacing=0 CELLPADDING=3 width=100% align=center>");
s.append("<tr>");

try {
RowBean rb = DbModel.getRowBean(sql);

for (int i = 0; i < rb.getColumnCount(); i++) {
s.append("<td class='x1281'>"
+ Normal.parse(rb.getColumnNames()[i]) + "</td>");
}
s.append("</tr>");

List v = rb.getList();
Iterator it = v.iterator();
while (it.hasNext()) {
MapUtil map = (MapUtil) it.next();
s.append("<tr>");
for (int j = 0; j < rb.getColumnCount(); j++) {
s.append("<td class='"
+ GetCss(Normal.parse(map
.get(rb.getColumnNames()[j])), Normal
.parse(rb.getColumnTypes()[j])) + "'>"
+ Normal.parse(map.get(rb.getColumnNames()[j]))
+ "</td>");
}
s.append("</tr>");
}
s.append("</table>");
} catch (Exception e)

{
s.append(e.toString());

}
return s;
}


/**
* 设置数据来源
*
* @param strSource
* @return
*/
public static StringBuffer GetFoot(String strSource) {
StringBuffer s = new StringBuffer();
if (strSource != null && !strSource.equals("")) {
s
.append("<table BORDER=0 CELLSPACING=1 CELLPADDING=3 width=100% align=center>"
+ "<tr><td height=\"30\" colspan=\"10\" style=\"border:0px\"></td></tr>"
+ "<tr><td colspan=\"10\" class=\"td_foot\" >数据来源:"
+ strSource + "</td></tr>" + "</table>");
s.append("</body></html>");
}
return s;
}

/**
* 设置单元格样式
*
* @param str
* @param typename
* @return
*/
public static String GetCss(String str, String typename) {
String tempStr = "x1282";
if (("Int32,Decimal,Double,NUMBER".indexOf(typename) >= 0)) {
tempStr = "x2";
}
return tempStr;
}
}
...全文
225 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
小_Y 2009-06-07
  • 打赏
  • 举报
回复
好贴。
qiheia 2009-06-06
  • 打赏
  • 举报
回复
好贴,值得收藏。。。
zyhowe 2009-06-06
  • 打赏
  • 举报
回复
不好意思,太长了,没COPY完,要看的去来源地看
weiluo12 2009-06-06
  • 打赏
  • 举报
回复
谢谢楼主!
收藏!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧