Java code Export Data to Excel, JS Export data to Excel (iii)

Source: Internet
Author: User

JSP content ignored, write only a start button:
<button style= "width:100px" onclick= "Expertexcel ()" >js export excel</button>&nbsp;&nbsp;&nbsp;
<button style= "width:100px" onclick= "Expertword ()" >js export word</button>&nbsp;&nbsp;&nbsp;
<button style= "width:100px" onclick= "GetDataToExcel2 ()" > Code Export </button>

<div class = ' main ' id= ' main ' ></div>
<div class = ' bottom ' id= ' buttom12 ' >
</div>

</div>
</body>
<script type= "Text/javascript" >


Background popup export data to Excel
function GetDataToExcel2 () {
var SSSJ = document.getElementById ("Time_ctrol"). Value;
var XTMC = document.getElementById ("St"). Value;

XTMC = encodeURI (encodeURI (XTMC));
var requesturl = "./datatoexcel.jsp?type=getdatatoexcel&sssj=" + SSSJ + "&xtmc=" + XTMC;
window.open (Requesturl);
}

function Createxmlhttprequest () {
if (window. ActiveXObject) {
Xmlhttpobject = new ActiveXObject ("Microsoft.XMLHTTP");
} else {
Xmlhttpobject = new XMLHttpRequest ();
}
return xmlhttpobject;
}








</script>//js Export Excel
<script type= "Text/javascript" >
function Expertexcel () {
var OXL = new ActiveXObject ("Excel.Application");
var owb = OXL.Workbooks.Add ();
var osheet = Owb.activesheet;
var sel=document.body.createtextrange ();
Sel.movetoelementtext (main);
Sel.select ();
Sel.execcommand ("Copy");
Osheet.paste ();
oXL.Visible = true;
}



Code Export page, copied from CSDN:
<%
It's not too scientific to write, because filename doesn't usually write dead.
Response.setheader ("Content-disposition", "attachment;filename= Aa.xls");
Response.setcontenttype ("application/vnd.ms-excel");
%><%@ page language= "java" import= "java.util.*" contenttype= "Application/vnd.ms-excel;charset=utf-8" pageencoding= "UTF-8"%>
<%@ page import= "Java.net.URLDecoder"%>
<%@ page import= "nariis.pi3000.ythtjfx.sbjk.pagehelp.*"%>
<%@ page import= "org.apache.poi.hssf.usermodel.*"%>
<%@ page import= "Java.util.ArrayList"%>
<%@ page import= "nariis.pi3000.ythTjFx.sbjk.entity.DataEntity"%><%
String type = Request.getparameter ("type");
String SSSJ = Request.getparameter ("SSSJ");
String XTMC = Request.getparameter ("XTMC");
XTMC = Urldecoder.decode (XTMC, "UTF-8");

System.out.print (type);
System.out.print (SSSJ);
System.out.print (XTMC);

String Path = "Excel"; Note, never write Chinese

Response.setheader ("Content-disposition", "attachment;filename=" + path + ". xls");

if ("Getdatatoexcel". Equals (type)) {
Pageservice pageservice = new Pageservice ();
arraylist<dataentity> list = Pageservice.getalldatatoexcel2 (SSSJ);

The first step is to create a webbook that corresponds to an Excel file
Hssfworkbook wb = new Hssfworkbook ();
In the second step, add a sheet in WebBook that corresponds to the sheet in the Excel file
Hssfsheet sheet = wb.createsheet ("Student form One");
In the third step, add the No. 0 row of the table header to the sheet, noting that the old version of POI has a limit on the number of rows in Excel short
Hssfrow row = Sheet.createrow ((int) 0);
Fourth step, create a cell, and set the value header to center the header
Hssfcellstyle style = Wb.createcellstyle ();
Style.setalignment (Hssfcellstyle.align_center); Create a center format

Hssfcell cell = Row.createcell ((short) 0);

Cell.setcellvalue ("SX");
Cell.setcellstyle (style);
Cell = Row.createcell ((short) 1);
Cell.setcellvalue ("DWMC");
Cell.setcellstyle (style);
Cell = Row.createcell ((short) 2);
Cell.setcellvalue ("sfgypp");
Cell.setcellstyle (style);
Cell = Row.createcell ((short) 3);
Cell.setcellvalue ("SJKLX");
Cell.setcellstyle (style);
Cell = Row.createcell ((short) 4);
Cell.setcellvalue ("Jsjsl");
Cell.setcellstyle (style);
Cell = Row.createcell ((short) 5);
Cell.setcellvalue ("SSSJ");
Cell.setcellstyle (style);
Cell = Row.createcell ((short) 6);
Cell.setcellvalue ("XTMC");
Cell.setcellstyle (style);
Cell = Row.createcell ((short) 7);

for (int i = 0; i < list.size (); i++)
{
int j = 0;
row = Sheet.createrow ((int) i + 1);
Dataentity dataentity = (dataentity) list.get (i);
Fourth step, create the cell and set the value
Row.createcell (short) j + +). Setcellvalue (Dataentity.getsx ());
Row.createcell (short) j + +). Setcellvalue (DATAENTITY.GETDWMC ());
Row.createcell (short) j + +). Setcellvalue (DATAENTITY.GETSFGYPP ());
Row.createcell (short) j + +). Setcellvalue (DATAENTITY.GETSJKLX ());
Row.createcell (short) j + +). Setcellvalue (Dataentity.getjsjsl ());
Row.createcell (short) j + +). Setcellvalue (DATAENTITY.GETSSSJ ());
Row.createcell (short) j + +). Setcellvalue (DATAENTITY.GETXTMC ());
}

Sixth, save the file to the specified location
Try
{
OutputStream Outp=response.getoutputstream ();
Servletoutputstream OUTP = Response.getoutputstream ();
Outp.flush ();
Wb.write (OUTP);
Outp.close ();
Response.flushbuffer ();
Out.clear ();
Out=pagecontext.pushbody ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}

%>

Java code Export Data to Excel, JS Export data to Excel (iii)

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.