Springboot & Mybatis Building RESTful Services four1 Front-facing conditions
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