JSP export Excel,word file several methods __js

Source: Internet
Author: User

1. Direct Output Table format

<%@ page language= "java" pageencoding= "GBK"%><%response.setheader ("content-disposition", "attachment; Filename=exdata.xls ");%>

<table id= "PrintA" border=1>
<tr class= "Line-odd" >
<TD align= "Right" > Support project Name: </TD><TD align= "left" > funded project name
</td>
</tr>
<tr class= "Line-even" >
<TD align= "Right" > Serial number: </TD><TD align= "left" > Serial number
</td>
</tr>
<tr class= "Line-odd" >
<TD align= "Right" > Support number: </TD><TD align= "Left" >
Funding number

</td>
</tr>

</table>

Note: garbled problem, page Save as Utf-8 format

Encoding format conversion

String s= "";

S=new String (s.getbytes ("gb2312"), "iso-8859-1")

2. Using JXL to generate Excel files

<%@ page language= "java" pageencoding= "GBK"%>
<% @page import= "Javax.servlet.ServletOutputStream"%>
<% @page import= "jxl.*"%>
<% @page import= "jxl.write.*"%>

<%

Response.setcontenttype ("Application/download");
Response.setheader ("Content-disposition", "Attachment;filename=totalexcel.xls");
Servletoutputstream SOS = Response.getoutputstream ();
Writableworkbook Wwb=null;

Writablecellformat Contentfromart = new Writablecellformat (numberformats.text);

WWB = Workbook.createworkbook (SOS);
Writablesheet ws = Wwb.createsheet ("Sheet1", 0);
Writablecellformat cellformat=new Writablecellformat (); Line Wrapping function
Cellformat.setalignment (Jxl.format.Alignment.LEFT);
Cellformat.setwrap (TRUE);
Label Lab = new label (0, 0, "ordinal");
Ws.addcell (Lab);
Lab = new Label (1, 0, "serial number", contentfromart);//Insert numeric string into lable
Ws.addcell (Lab);
Lab = new Label (2, 0, "project number");
Ws.addcell (Lab);
Lab = new Label (3, 0, "funded project name");
Ws.addcell (Lab);
Lab = new Label (4, 0, "project start date");
Ws.addcell (Lab);
Lab = new Label (5, 0, "Amount");
Ws.addcell (Lab);
Lab = new Label (6, 0, "owner");
Ws.addcell (Lab);
Lab = new Label (7, 0, "Fill out a person");
Ws.addcell (Lab);
Lab = new Label (8, 0, "Grant unit");
Ws.addcell (Lab);

Wwb.write ();
Wwb.close ();
Sos.flush ();
Sos.close ();
Out.clear ();
out = Pagecontext.pushbody ();

%>

3.js Export to Excel (requires server to install Office). Because of the use of the Excel.Application control, you have to reduce the security level of the computer ie. Otherwise, the "Automation server cannot create object" error is reported.

Method One:
function Allareaexcel (tableid) {//Full table Copy to Excel
var printa= document.getElementById (TableID);
var oXL = new ActiveXObject ("Excel.Application");
Create an Ax object Excel
var owb = OXL.Workbooks.Add ();
Get Workbook Object
var osheet = Owb.activesheet;
Activate current sheet
var sel = Document.body.createTextRange ();
Sel.movetoelementtext (PrintA);
Move the contents of the table to TextRange
Sel.select ();
Full selection of TextRange contents
Sel.execcommand ("Copy");
Copy content in TextRange
Osheet.paste ();
Paste into active Excel
oXL.Visible = true;
Set Excel Visible Properties
}
Specify the page area cell content import Excel
function Cellareaexcel (TableID)
{

var printa= document.getElementById (TableID);
var oXL = new ActiveXObject ("Excel.Application");
var owb = OXL.Workbooks.Add ();
var osheet = Owb.activesheet;
var lenr = PrintA.rows.length;
for (i=0;i<lenr;i++)
{
var Lenc = printa.rows (i). Cells.length;
for (j=0;j<lenc;j++)
{
Osheet.cells (i+1,j+1). Value = Printa.rows (i). Cells (j). innertext;
}
}
oXL.Visible = true;
}

4.JSP Export to Word

<%@ page language= "java" pageencoding= "GBK"%><% @pagecontentType = "APPLICATION/VND.MS-WORD;CHARSET=GBK"% ><%response.setheader ("Content-disposition", "Attachment Filename=exdata.doc");%>

<table id= "PrintA" border=1>
<tr class= "Line-odd" >
<TD align= "Right" > Support project Name: </TD><TD align= "left" > funded project name
</td>
</tr>
<tr class= "Line-even" >
<TD align= "Right" > Serial number: </TD><TD align= "left" > Serial number
</td>
</tr>
<tr class= "Line-odd" >
<TD align= "Right" > Support number: </TD><TD align= "Left" >
Funding number

</td>
</tr>

</table>

5.js Export to Word

Specify page area content import Word
function Allareaword (TableID)
{

var printa= document.getElementById (TableID);
var OWD = new ActiveXObject ("Word.Application");
var ODC = OWD.Documents.Add ("", 0, 1);
var oRange =odc.range (0,1);
var sel = Document.body.createTextRange ();
Sel.movetoelementtext (PrintA);
Sel.select ();
Sel.execcommand ("Copy");
Orange.paste ();
OWD.Application.Visible = true;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.