Favorites -- export data from Java to excel and save it to the server and client for download

Source: Internet
Author: User

Example :( from: http://www.iteye.com/problems/54701)

Import Java. io. file; import Java. io. fileoutputstream; import Java. io. outputstream; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; import jxl. workbook; import jxl. write. label; import jxl. write. writablecellformat; import jxl. write. writablefont; import jxl. write. writablesheet; import jxl. write. writableworkbook; Public Cl Ass testexcel {private string driverclass = "net. sourceForge. jtds. JDBC. driver "; private string url =" JDBC: jtds: sqlserver:/localhost: 1433/demo "; private string user =" sa "; private string Password = ""; private connection; Public void exportclassroom (outputstream OS) {try {writableworkbook wbook = workbook. createworkbook (OS); // create an Excel file writablesheet wsheet = wbook. createsheet ("Alarm record table ", 0); // worksheet name // set the Excel font writablefont wfont = new writablefont (writablefont. arial, 16, writablefont. bold, false, jxl. format. underlinestyle. no_underline, jxl. format. colour. black); writablecellformat titleformat = new writablecellformat (wfont); string [] Title = {"Alarm record number", "alarm time", "alarm device", "alarm device number ", "alert event name", "Alert No."}; // set the Excel header for (INT I = 0; I <title. length; I ++) {label exceltitle = new label (I, 0, title [I], titleformat); wsheet. addcell (exceltitle);} int c = 1; // The Excel row number connection con = openconnection (); statement ST = con. createstatement (); string SQL = "select * From jlbjsj where bjjlsj between '2017-09-01 08:19:25 'and '2017-12-02 08:19:25' and bjsbmc like '% '"; resultset rs=st.exe cutequery (SQL); // This is the data to be exported from the database while (RS. next () {label content1 = new label (0, C, (string) rs. getstrin G ("bjjlbh"); label content2 = new label (1, C, (string) rs. getstring ("bjjlsj"); label content3 = new label (2, C, (string) rs. getstring ("bjsbmc"); label content4 = new label (3, C, (string) rs. getstring ("bjsbbh"); label content5 = new label (4, C, (string) rs. getstring ("bjblmc"); label content6 = new label (5, C, (string) rs. getstring ("bjblbh"); wsheet. addcell (content1); wsheet. addcell (content2); wsheet. Addcell (content3); wsheet. addcell (content4); wsheet. addcell (content5); wsheet. addcell (content6); C ++;} wbook. write (); // write the wbook file. close (); OS. close (); system. out. println ("Import successful! ");} Catch (exception e) {e. printstacktrace () ;}} public connection openconnection () throws sqlexception {try {class. forname (driverclass ). newinstance (); connection = drivermanager. getconnection (URL, user, password); return connection;} catch (exception e) {Throw new sqlexception (E. getmessage () ;}} public void closeconnection () {try {If (connection! = NULL) connection. close ();} catch (exception e) {e. printstacktrace () ;}}// the main method of the portal public static void main (string [] ARGs) {testexcel TE = new testexcel (); file F = new file ("D: \ k.xls "); try {f. createnewfile (); outputstream OS = new fileoutputstream (f); Te. exportclassroom (OS);} catch (exception e) {e. printstacktrace ();}}}

Java File Download Methods

Http://yaofeng911.iteye.com/blog/472492

 

Download files from the server-Java-only processing

Http://shuixian.iteye.com/blog/706870

 

JSP downloads the xls file from the server to the client

Http://yuanyuan7891.iteye.com/blog/723483

 

JSP downloads the xls file from the server to the client

Http://yuanyuan7891.iteye.com/blog/723483

 

Full access to Java paths

Http://www.360doc.com/content/08/0327/19/3123_1148156.shtml

 

Export an Excel file from the server to a local device

Http://blog.csdn.net/xinguojava/article/details/6256538

 

Upload and download files in Linux (simple commands are worth your reference)

Http://wangjinlongaisong-126-com.iteye.com/blog/1464708

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.