An instance of exporting an Excel template

Source: Internet
Author: User
Tags log log

1 JSP page First

Click on the template download, will automatically download the template Excel, the effect is as follows

Let's look at the source code:

1 JSP page

<div class= "Tab-pane" id= "Profile" >
&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type= "button" id= "Export" onclick= "Tomodelexport ()" value= "template Download" >
&nbsp;&nbsp;&nbsp;&nbsp;
<input type= "button" id= "Export" onclick= "Toexport ()" value= "Bulk Import" >
</div>

It's mostly two buttons.

JS function

function Tomodelexport () {

$ ("<form>"). attr ({
"Action": "${ctx}/doctor/tomodeldownload",
"Method": "Get"
}). submit (); Tomodelexport

}

2 Backend Controller

Template Import
@RequestMapping (value = "/tomodeldownload")
public void Tomodeldownload (HttpServletRequest request, httpservletresponse response) {
Writableworkbook wb = null;
OutputStream OS = null;
String fileName = "Doctorimportstand.xls";
try {
OS = Response.getoutputstream ();
WB =new Importmodel (). Createworkbookmodel (OS);
Response.setcontenttype ("application/vnd.ms-excel");
Response.setheader ("Content-disposition", "attachment; Filename=\ "" + fileName + "\" ");
Wb.write ();
Wb.close ();
Os.close ();
Response.flushbuffer ();
} catch (Exception e) {
E.printstacktrace ();
}
}

The core is this new Importmodel (). Createworkbookmodel (OS);

3Importmodel class

public class Importmodel {
Protected final Log log = Logfactory.getlog (GetClass ());
Doctor Templates
Public Writableworkbook Createworkbookmodel (OutputStream outputstream) {
Writableworkbook WWB = null;
try {
WWB = Workbook.createworkbook (OutputStream);
Writablesheet sheet =wwb.createsheet ("Doctor Information Entry Template", 1);
Fix JXL drop-down box 254 character limit
Writablesheet Sheet2 = Wwb.createsheet ("entry Instructions", 1);
Writablefont font = new Writablefont (jxl.write.WritableFont
. CreateFont ("Official script"), 14);
Writablecellformat format = new Writablecellformat (font);
Label Label1 = new label (0,0, "Gender", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,1, "1", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 1, "male", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,2, "2", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 2, "female", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,3, "User type", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,4, "1", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 4, "Doctor", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,5, "2", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 5, "patient", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,6, "3", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 6, "expert", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,7, "title", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,8, "1", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 8, "Chief physician", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,9, "2", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 9, "deputy chief physician", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,10, "3", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 10, "Attending physician", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,11, "4", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 11, "physician", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,12, "5", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 12, "Professor", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,13, "6", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 13, "Associate Professor", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,14, "Hospital Code", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,15, "1", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 15, "Zhongshan Hospital", format);
Sheet2.addcell (Label1);

Label1 = new Label (0,16, "2", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 16, "Kanazawa Town Community Health Service Center", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,17, "3", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 17, "Qingpu Center Hospital", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,18, "organization structure", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,19, "1", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 19, "Lung Cancer Federation", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,20, "department", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,21, "1", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 21, "respiratory Medicine", format);
Sheet2.addcell (Label1);
Label1 = new Label (0,22, "2", format);
Sheet2.addcell (Label1);
Label1 = new Label (1, 22, "whole family", format);
Sheet2.addcell (Label1);
Sheet.mergecells (0,0,13,0);
Sheet.setrowview (0, 400);
StringBuffer strbuf = new StringBuffer ();
Strbuf.append ("Property Description: \ r \ n 1, User type: 1 Doctor 2 patient 3 expert here can only import Doctor")
. Append ("\ r \ n"). Append ("2, Gender: 1 male 2 female")
. Append ("\ r \ n"). Append ("3, organization structure for user's organization ID 1 lung Cancer Alliance")
. Append ("\ r \ n"). Append ("4, hospital, department all enter the number can")
. Append ("\ r \ n"). Append ("5, Title: 1 Chief Physician 2 Deputy Chief Physician 3 attending physician 4 Physician 5 Professor 6 Associate professor")
. Append ("\ r \ n"). Append ("6, please strictly follow the template format for import, each item must be filled, otherwise you can not import");
Label label = new label (0,0,strbuf.tostring ());
Sheet.addcell (label);
Label = new label (0,1, "user type");
Sheet.addcell (label);
Label = new label (0,2, "1");
Sheet.addcell (label);
Label = new label (1, 1, "phone number");
Sheet.addcell (label);
Label = new label (1, 2, "13524929813");
Sheet.addcell (label);
Label = new label (2,1, "name");
Sheet.addcell (label);
Label = new label (2,2, "Zhao");
Sheet.addcell (label);
Label = new label (3,1, "gender");
Sheet.addcell (label);
Label = new label (3,2, "2");
Sheet.addcell (label);
Label = new label (4,1, "organization structure");
Sheet.addcell (label);
Label = new label (4,2, "1");
Sheet.addcell (label);
Label = new label (5,1, "Hospital");
Sheet.addcell (label);
Label = new label (5,2, "2");
Sheet.addcell (label);
Label = new label (6,1, "department");
Sheet.addcell (label);
Label = new label (6,2, "2");
Sheet.addcell (label);
Label = new label (7,1, "title");
Sheet.addcell (label);
Label = new label (7,2, "1");
Sheet.addcell (label);
Label = new label (8,1, "date of birth");
Sheet.addcell (label);
Label = new label (8,2, "1980.08.08");
Sheet.addcell (label);
Label = new label (9,1, "Doctor Role");
Sheet.addcell (label);
Label = new label (9,2, "3");
Sheet.addcell (label);
Label = new label (10,1, "Sitting Time");
Sheet.addcell (label);
Label = new label (10,2, "1-0,1-1,2-0,2-1");
Sheet.addcell (label);
Label = new label (11,1, "consultation Time");
Sheet.addcell (label);
Label = new label (11,2, "3");
Sheet.addcell (label);
Label = new label (12,1, "consultation price");
Sheet.addcell (label);
Label = new label (12,2, "2000");
Sheet.addcell (label);
Label = new label (13,1, "Introduction");
Sheet.addcell (label);
Label = new label (13,2, "Associate professor, Doctor of Medicine, executive Director of Respiratory medicine, QINGPU discipline leader, youth member of Shanghai Medical Association Lung Science Society". Engaged in the clinical medical, scientific research and teaching of the Department of Respiratory Medicine for more than 20 years, solid theoretical knowledge, good at chronic obstructive pulmonary disease, bronchial asthma, pulmonary heart disease, respiratory failure, pulmonary infection disease, lung tumors, interstitial lung disease, pleural diseases and other respiratory diseases, for the diagnosis and treatment of respiratory problems, The diagnosis, treatment and rescue techniques of critically ill patients, mechanical ventilation technology and respiratory endoscopy and interventional diagnosis and treatment technology have rich clinical experience. In recent years, presided over 2 municipal scientific research projects, participated in the National Natural Fund projects, municipal scientific research project items; As the first author in sci and domestic core journals published more than 20 professional academic papers. ");
Sheet.addcell (label);
} catch (IOException e) {
Log.error ("Error:createworkbookmodel", e);
} catch (Rowsexceededexception e) {
Log.error ("Error:createworkbookmodel", e);
} catch (WriteException e) {
Log.error ("Error:createworkbookmodel", e);
}
return WWB;
}

At this point, the function can be implemented smoothly

The dependencies that are used

Import java.io.IOException;
Import Java.io.OutputStream;

Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;

Import JXL. Workbook;
Import Jxl.write.Label;
Import Jxl.write.WritableCellFormat;
Import Jxl.write.WritableFont;
Import Jxl.write.WritableSheet;
Import Jxl.write.WritableWorkbook;
Import jxl.write.WriteException;
Import jxl.write.biff.RowsExceededException;

An instance of exporting an Excel template

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.