Commit 66c7aed3 by shj

输出word文档,使用apache poi库,依次为每种费用生成说明

parent 2a71c759
......@@ -148,6 +148,20 @@
<version>2.8.5</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -16,6 +16,7 @@ import org.springframework.web.servlet.ModelAndView;
import service.IBudgetService;
import service.ICheckService;
import service.IUserBudgetService;
import util.BudgetExportUtil;
import util.CookieUtil;
import javax.servlet.ServletOutputStream;
......@@ -201,6 +202,34 @@ public class BudgetHandler {
}
}
/**
* 下载word说明
* @param budgetid
* @param request
* @param response
* @param session
*/
@RequestMapping("/Download/word/{budgetid}")
public void downloadWord(@PathVariable("budgetid") Long budgetid,HttpServletRequest request, HttpServletResponse response, HttpSession session) {
//response.setContentType("application/octet-stream");
response.setContentType("text/html;charset=UTF-8");
String sessionID = budgetid.toString();
try {
Budget budget = retrieveBudget(sessionID);
//writer = response.getWriter();
response.setHeader("content-disposition", "attachment;filename=Budget" + sessionID + ".docx");
assert budget != null;
//budgetToOutputStream(budget, writer);
//budgetToOutputStream(budget, response.getOutputStream());
BudgetExportUtil.toWord(budget,response.getOutputStream());
System.out.println("Download word...............");
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}
@RequestMapping("/Detail")
......
......@@ -84,10 +84,10 @@
</a>
<button class="btn btn-info">修改属性</button>
<a v-bind:href="'${pageContext.request.contextPath}/Budget/Download/csv/'+budget.id">
<button class="btn btn-success">下载csv</button>
<button class="btn btn-success">下载费用清单</button>
</a>
<a v-bind:href="'${pageContext.request.contextPath}/Budget/Download/word/'+budget.id">
<button class="btn btn-success">下载word</button>
<button class="btn btn-success">下载费用说明</button>
</a>
<button class="btn btn-danger" @click="del(budget.id)">删除</button>
</td>
......
import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
public class PoiTest {
@Test
public void test01() throws IOException {
XWPFDocument document = new XWPFDocument();
FileOutputStream out = new FileOutputStream("F:/new.docx");
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("一、设备费");
paragraph=document.createParagraph();
run=paragraph.createRun();
run.setText("北京航空航天大学拟购置1类设备3台(套),总价格20万元。");
XWPFTable table = document.createTable(3,8);
List<XWPFTableRow> rows = table.getRows();
for (XWPFTableRow row : rows) {
List<XWPFTableCell> tableCells = row.getTableCells();
for (XWPFTableCell tableCell : tableCells) {
tableCell.setText("啊啊啊啊");
}
}
document.write(out);
out.close();
}
class Student{
private int id;
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment