Springboot & Mybatis Building RESTful Services Four

Source: Internet
Author: User
Tags getmessage

Springboot & Mybatis Building RESTful Services four1 Front-facing conditions
    • Successful completion of Springboot & Mybatis building RESTful Services Three

2 RESTful service add Apache poi generate Excel file1) Modify the Pom.xml file

Add dependencies for Apache POI

        <dependency>
             <groupId>org.apache.poi</groupId>  
             <artifactId>poi-ooxml</artifactId>  
             <version>3.5-FINAL</version>  
        </dependency>
2) Add a method to create an Excel file in Sy

? Src/main/java/com/ejtone/controller/sy.java

    @ApiOperation (value= "Query all account balances and other information and export them in Excel table")
    @RequestMapping (value= "/accountsfile", method = Requestmethod.get)
    public boolean Wselectall (HttpServletResponse response) {
        
        list<settleaccount> list = null;
        Boolean flag = false;
        Get all account information
        List = Isy.selectaccounts ();
        Determine if an account exists
        if (list = = null)
            Logg.warn ("Not Found accounts");
        else{
            To create a Table object
            Hssfworkbook WorkBook = new Hssfworkbook ();
            Servletoutputstream fileout = null;
            try {
                Get output Stream Object
                Fileout = Response.getoutputstream ();
                Create sheet
                Hssfsheet sheet = Workbook.createsheet ();
                Set the field name for the first row of the table
                Hssfrow row = sheet.createrow (0);
                int i = 1;
                Row.createcell (0). Setcellvalue ("account number");
                Row.createcell (1). Setcellvalue ("account name");
                Row.createcell (2). Setcellvalue ("customer number");
                Row.createcell (3). Setcellvalue ("SMS Balance");
                for (Settleaccount acc:list) {
                    Hssfrow rowi = Sheet.createrow (i++);
                    Rowi.createcell (0). Setcellvalue (Acc.getaccountcode ());
                    Rowi.createcell (1). Setcellvalue (Acc.getaccountname ());
                    Rowi.createcell (2). Setcellvalue (Acc.getcustomercode ());
                    Rowi.createcell (3). Setcellvalue (Acc.getsmsnum (). Doublevalue ());
                }
                Workbook.write (fileout);
                return true;
            } catch (FileNotFoundException e) {
                Logg.error ("CREATE TABLE error----" + e.getmessage ());
                E.printstacktrace ();
                return false;
            } catch (IOException e) {
                Logg.error ("CREATE TABLE error----" + e.getmessage ());
                E.printstacktrace ();
                return false;
            } finally {
                try {
                    Fileout.close ();
                } catch (IOException e) {
                    E.printstacktrace ();
                }
            }
        }
        return flag;
    }
5) Enter the following test instructions at the terminal:
directory where #cd project is located
Cd/users/pengsijia/documents/pro/xm/accountbalance
MVN Clean Package
CD target
mkdir/users/pengsijia/desktop/t/
Copy #将 tar package to its own specified directory (/users/pengsijia/desktop/t/)
CP accountbalance-0.0.1-snapshot.tar/users/pengsijia/desktop/t/
#cd to the directory specified in the previous operation
cd/users/pengsijia/desktop/t
#解压 Tar Package
TAR-XVF Accountbalance-0.0.1-snapshot.tar
#此时可查看目录结构如要求所示
ll
#运行 executable jar, test results
Java-jar Accountbalance-0.0.1-snapshot.jar
#
#打开浏览器
Http://localhost:8999/accountsfile
#在下载对话框中指定文件名和保存路径
#
#返回上个 iterm window, Control+c end service

Springboot & Mybatis Building RESTful Services Four

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.