从网上找了个例子改了2天没实现自己的效果!快崩溃 了!!/*
以下这样在jsp的一个table生成excel
格式具体如下
1、类别为A的方在一个sheet 里并设置sheetName = "A";
2、同理B类和C类都一样用jxl怎么写
table
td1 td2 td3 类别
1 2 3 A
1 2 1 A
1 2 3 A
1 1 3 B
1 4 3 B
1 2 4 B
1 4 3 C
1 4 1 C
1 4 4 C
*/<script type="text/javascript">
function exportExcel(id){
var forum = document.forms["ExcelExportor"];
forum.c.value=eval(id+".innerHTML");
forum.submit();
}
</script>
<form name="ExcelExportor" method="OST" action="/export/excel.jsp">
<input type="hidden" name="c" />
<input onclick="exportExcel('MAIN_TABLE')" type="button" class="mybutton" value="导出Excel">
</form>
<!-- 下面是你要导出的表格,其id必须和前面的相同 -->
<table border=0 cellpadding="2" cellspacing="1" id="MAIN_TABLE">
<tr class="title">
<tr class="title">
<td>規格</td>
<td>數量</td>
<td>日期</td>
<td>類別</td>
</tr>
</tr>
...
</table>public class ExcelExportor extends HttpServlet {
private static final long serialVersionUID = 8563623076707865788L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("GBK");
String content = request.getParameter("c");
if (content == null) {
Writer out = response.getWriter();
response.setCharacterEncoding("GBK");
out.write("<html><body>No Content</body></html>");
return;
}
try {
export(content, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private WritableWorkbook wwb = null;
private WritableSheet sheet = null;
private void export(String content, HttpServletResponse response) throws IOException, RowsExceededException, WriteException {
response.setContentType("application/ms-excel");
String sheetName = getCaption(content);
if (sheetName == null) {
sheetName = "Sheet1";
}
sheetName = sheetName.replaceAll(":", "").replaceAll("[)]", "").replaceAll("[(]", "");
response.addHeader("Content-Disposition", "attachment; filename=" + new String(sheetName.getBytes("GBK"), "ISO-8859-1")
+ ".xls");
OutputStream os = response.getOutputStream();
wwb = Workbook.createWorkbook(os);
wwb.setProtected(true);
sheet = wwb.createSheet(sheetName, 0);
int row = 0;
int col = 0;
Label label = null;
if (sheetName.trim().length() > 30) {
label = new Label(col, row, sheetName);
sheet.addCell(label);
row++;
}
List<TD> listBody = getContent(content);
Map<String, Boolean> map = new HashMap<String, Boolean>();
for (TD td : listBody) {
if (td == null) {
row++;
col = 0;
continue;
}
while (map.get(col + "-" + row) != null) {
col++;
}
if (td.colspan > 1 || td.rowspan > 1) {
sheet.mergeCells(col, row, col + td.colspan - 1, row + td.rowspan - 1);
for (int i = col; i <= col + td.colspan - 1; i++) {
for (int j = row; j <= row + td.rowspan - 1; j++) {
map.put(i + "-" + j, true);
}
}
}
label = new Label(col, row, td.content);
sheet.addCell(label); map.put(col + "-" + row, true);
col += td.colspan;
}
wwb.write();
wwb.close();
}
private String getCaption(String content) {
int begin = content.indexOf("<CAPTION");
int end = content.indexOf("</CAPTION>");
if (begin == -1 || end == -1) {
return null;
}
begin = content.indexOf(">", begin);
if (begin == -1) {
return null;
}
return content.substring(begin + 1, end);
}
public List<TD> getContent(String content) throws UnsupportedEncodingException {
int begin = -1;
int end = -1;
int index = -1;
String numberStr;
int number;
String[] tables = content.split("</TABLE>");
List<TD> list = new ArrayList<TD>();
for (String table : tables) {
String[] trs = table.split("</TR>");
for (String tr : trs) {
number = 1;
String[] ss = tr.split("</TD>");
for (String s : ss) {
begin = s.indexOf("<TD");
if (begin == -1) {
continue;
}
s = s.substring(begin + 3);
index = s.indexOf(">");
TD td = new TD();
begin = s.indexOf("rowSpan=");
if (begin != -1) {
end = s.indexOf(" ", begin);
if (end == -1) {
end = index;
}
numberStr = s.substring(begin + 8, end).replace('"', ' ').replace('\'', ' ').trim();
number = Integer.parseInt(numberStr);
td.rowspan = number;
}
begin = s.indexOf("colSpan=");
if (begin != -1) {
end = s.indexOf(" ", begin);
index = s.indexOf(">", begin);
if (end == -1) {
end = index;
}
if (end > index) {
end = index;
}
numberStr = s.substring(begin + 8, end).replace('"', ' ').replace('\'', ' ').trim();
number = Integer.parseInt(numberStr);
td.colspan = number;
}
td.content = s.substring(index + 1).replaceAll("\\<.*?\\>", "").replaceAll(" ", "").trim();
list.add(td);
}
list.add(null);
}
list.add(null);
list.add(null);
}
return list;
}
}
class TD {
int rowspan = 1;
int colspan = 1;
String content;
}
以下这样在jsp的一个table生成excel
格式具体如下
1、类别为A的方在一个sheet 里并设置sheetName = "A";
2、同理B类和C类都一样用jxl怎么写
table
td1 td2 td3 类别
1 2 3 A
1 2 1 A
1 2 3 A
1 1 3 B
1 4 3 B
1 2 4 B
1 4 3 C
1 4 1 C
1 4 4 C
*/<script type="text/javascript">
function exportExcel(id){
var forum = document.forms["ExcelExportor"];
forum.c.value=eval(id+".innerHTML");
forum.submit();
}
</script>
<form name="ExcelExportor" method="OST" action="/export/excel.jsp">
<input type="hidden" name="c" />
<input onclick="exportExcel('MAIN_TABLE')" type="button" class="mybutton" value="导出Excel">
</form>
<!-- 下面是你要导出的表格,其id必须和前面的相同 -->
<table border=0 cellpadding="2" cellspacing="1" id="MAIN_TABLE">
<tr class="title">
<tr class="title">
<td>規格</td>
<td>數量</td>
<td>日期</td>
<td>類別</td>
</tr>
</tr>
...
</table>public class ExcelExportor extends HttpServlet {
private static final long serialVersionUID = 8563623076707865788L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("GBK");
String content = request.getParameter("c");
if (content == null) {
Writer out = response.getWriter();
response.setCharacterEncoding("GBK");
out.write("<html><body>No Content</body></html>");
return;
}
try {
export(content, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private WritableWorkbook wwb = null;
private WritableSheet sheet = null;
private void export(String content, HttpServletResponse response) throws IOException, RowsExceededException, WriteException {
response.setContentType("application/ms-excel");
String sheetName = getCaption(content);
if (sheetName == null) {
sheetName = "Sheet1";
}
sheetName = sheetName.replaceAll(":", "").replaceAll("[)]", "").replaceAll("[(]", "");
response.addHeader("Content-Disposition", "attachment; filename=" + new String(sheetName.getBytes("GBK"), "ISO-8859-1")
+ ".xls");
OutputStream os = response.getOutputStream();
wwb = Workbook.createWorkbook(os);
wwb.setProtected(true);
sheet = wwb.createSheet(sheetName, 0);
int row = 0;
int col = 0;
Label label = null;
if (sheetName.trim().length() > 30) {
label = new Label(col, row, sheetName);
sheet.addCell(label);
row++;
}
List<TD> listBody = getContent(content);
Map<String, Boolean> map = new HashMap<String, Boolean>();
for (TD td : listBody) {
if (td == null) {
row++;
col = 0;
continue;
}
while (map.get(col + "-" + row) != null) {
col++;
}
if (td.colspan > 1 || td.rowspan > 1) {
sheet.mergeCells(col, row, col + td.colspan - 1, row + td.rowspan - 1);
for (int i = col; i <= col + td.colspan - 1; i++) {
for (int j = row; j <= row + td.rowspan - 1; j++) {
map.put(i + "-" + j, true);
}
}
}
label = new Label(col, row, td.content);
sheet.addCell(label); map.put(col + "-" + row, true);
col += td.colspan;
}
wwb.write();
wwb.close();
}
private String getCaption(String content) {
int begin = content.indexOf("<CAPTION");
int end = content.indexOf("</CAPTION>");
if (begin == -1 || end == -1) {
return null;
}
begin = content.indexOf(">", begin);
if (begin == -1) {
return null;
}
return content.substring(begin + 1, end);
}
public List<TD> getContent(String content) throws UnsupportedEncodingException {
int begin = -1;
int end = -1;
int index = -1;
String numberStr;
int number;
String[] tables = content.split("</TABLE>");
List<TD> list = new ArrayList<TD>();
for (String table : tables) {
String[] trs = table.split("</TR>");
for (String tr : trs) {
number = 1;
String[] ss = tr.split("</TD>");
for (String s : ss) {
begin = s.indexOf("<TD");
if (begin == -1) {
continue;
}
s = s.substring(begin + 3);
index = s.indexOf(">");
TD td = new TD();
begin = s.indexOf("rowSpan=");
if (begin != -1) {
end = s.indexOf(" ", begin);
if (end == -1) {
end = index;
}
numberStr = s.substring(begin + 8, end).replace('"', ' ').replace('\'', ' ').trim();
number = Integer.parseInt(numberStr);
td.rowspan = number;
}
begin = s.indexOf("colSpan=");
if (begin != -1) {
end = s.indexOf(" ", begin);
index = s.indexOf(">", begin);
if (end == -1) {
end = index;
}
if (end > index) {
end = index;
}
numberStr = s.substring(begin + 8, end).replace('"', ' ').replace('\'', ' ').trim();
number = Integer.parseInt(numberStr);
td.colspan = number;
}
td.content = s.substring(index + 1).replaceAll("\\<.*?\\>", "").replaceAll(" ", "").trim();
list.add(td);
}
list.add(null);
}
list.add(null);
list.add(null);
}
return list;
}
}
class TD {
int rowspan = 1;
int colspan = 1;
String content;
}
解决方案 »
- 问大家一个公共问题。。。。。。。
- showModalDialog打印问题
- 多页面传值问题
- 《工作流技术探讨》--支持浏览器上,图形化自定义流程。
- 关于加号展开问题
- servlet的destroy函数里面怎么不能终止我在init函数里启动的几个线程?急
- out.println()问题
- 如何用代码实现excel表导入到access数据库中?
- 同一IP地址,不同端口的两个Server间post后引起的Session丢失的问题,急
- 用JAVA访问SQL SERVER需要在JDK中加什么东西吗?若不需要为什么下面的程序访问不了数据库?
- 求jsp用application 统计访问人数的代码
- 很简单,大家帮忙看看,在servlet里怎样正确的向页面输出<%out.println(exception.getMessage();)%>信息
那个回个结了!