THƯ VIỆN
dependency>
groupId>javax.portlet/groupId>
artifactId>portlet-api/artifactId>
version>2.0/version>
scope>provided/scope>
/dependency>
dependency>
groupId>com.liferay.portal/groupId>
artifactId>portal-service/artifactId>
version>${liferay.version}/version>
scope>provided/scope>
/dependency>
dependency>
groupId>com.liferay.portal/groupId>
artifactId>portal-impl/artifactId>
version>${liferay.version}/version>
scope>provided/scope>
/dependency>
TRONG BEAN
dependency>
groupId>javax.portlet/groupId>
artifactId>portlet-api/artifactId>
version>2.0/version>
scope>provided/scope>
/dependency>
dependency>
groupId>com.liferay.portal/groupId>
artifactId>portal-service/artifactId>
version>${liferay.version}/version>
scope>provided/scope>
/dependency>
dependency>
groupId>com.liferay.portal/groupId>
artifactId>portal-impl/artifactId>
version>${liferay.version}/version>
scope>provided/scope>
/dependency>
TRONG BEAN
public void exportToExcel() {
CellsDataTableMapThongKeHangMucTheoNam cellsDataTableMapThongKeHangMucTheoNam_Moi = new CellsDataTableMapThongKeHangMucTheoNam(hangMucMois, 0);
CellsDataTableMapThongKeHangMucTheoNam cellsDataTableMapThongKeHangMucTheoNam_ChuyenTiep = new CellsDataTableMapThongKeHangMucTheoNam(hangMucChuyenTieps, hangMucMois.size());
WorkbookDesigner designer = new WorkbookDesigner();
try {
String templatePath = FacesContext.getCurrentInstance()
.getExternalContext().getRealPath("/") + File.separator + "TemplateExports" + File.separator + "thongketheonam_in.xlsx";
Workbook workbook = new Workbook(templatePath);
designer.setWorkbook(workbook);
designer.setDataSource("title", "BÁO CÁO HẠNG MỤC SỬ DỤNG NGÂN SÁCH SỰ NGHIỆP THEO NĂM " + namSearch);
designer.setDataSource("namGiaiNgan", "Giải Ngân Năm " + namSearch);
designer.setDataSource("hangmucmoi", cellsDataTableMapThongKeHangMucTheoNam_Moi);
designer.setDataSource("tongHangMucMoi", hangMucMois.size());
designer.setDataSource("tongHangMucChuyenTiep", hangMucChuyenTieps.size());
designer.setDataSource("tongHangMuc", hangMucMois.size() + hangMucChuyenTieps.size());
designer.setDataSource("tongMucDauTuHangMucMoi", tongTongMucDauTuHMMoi);
designer.setDataSource("tongMucDauTuHangMucChuyenTiep", tongTongMucDauTuHMChuyenTiep);
designer.setDataSource("tongMucDauTuHangMuc", tongTongMucDauTuHMMoi + tongTongMucDauTuHMChuyenTiep);
designer.setDataSource("hangmucchuyentiep", cellsDataTableMapThongKeHangMucTheoNam_ChuyenTiep);
designer.process(false);
ByteArrayOutputStream ous = new ByteArrayOutputStream();
designer.getWorkbook().save(ous, SaveFormat.XLSX);
PortletResponse portletResponse = context.getPortletResponse();
HttpServletResponse res = PortalUtil.getHttpServletResponse(portletResponse);
res.setHeader("Content-Disposition", "attachment; filename=\"" + "thongkedanhmuchangmuc.xlsx" + "\"");
res.setHeader("Content-Transfer-Encoding", "binary");
res.setContentType("Content-type: application/vnd.ms-excel");
res.flushBuffer();
OutputStream out = res.getOutputStream();
byte[] buffer = new byte[4096];
int byteread;
InputStream is = new ByteArrayInputStream(ous.toByteArray());
while ((byteread = is.read(buffer)) != -1) {
out.write(buffer, 0, byteread);
buffer = new byte[4096];
}
out.flush();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
CellsDataTableMapThongKeHangMucTheoNam cellsDataTableMapThongKeHangMucTheoNam_Moi = new CellsDataTableMapThongKeHangMucTheoNam(hangMucMois, 0);
CellsDataTableMapThongKeHangMucTheoNam cellsDataTableMapThongKeHangMucTheoNam_ChuyenTiep = new CellsDataTableMapThongKeHangMucTheoNam(hangMucChuyenTieps, hangMucMois.size());
WorkbookDesigner designer = new WorkbookDesigner();
try {
String templatePath = FacesContext.getCurrentInstance()
.getExternalContext().getRealPath("/") + File.separator + "TemplateExports" + File.separator + "thongketheonam_in.xlsx";
Workbook workbook = new Workbook(templatePath);
designer.setWorkbook(workbook);
designer.setDataSource("title", "BÁO CÁO HẠNG MỤC SỬ DỤNG NGÂN SÁCH SỰ NGHIỆP THEO NĂM " + namSearch);
designer.setDataSource("namGiaiNgan", "Giải Ngân Năm " + namSearch);
designer.setDataSource("hangmucmoi", cellsDataTableMapThongKeHangMucTheoNam_Moi);
designer.setDataSource("tongHangMucMoi", hangMucMois.size());
designer.setDataSource("tongHangMucChuyenTiep", hangMucChuyenTieps.size());
designer.setDataSource("tongHangMuc", hangMucMois.size() + hangMucChuyenTieps.size());
designer.setDataSource("tongMucDauTuHangMucMoi", tongTongMucDauTuHMMoi);
designer.setDataSource("tongMucDauTuHangMucChuyenTiep", tongTongMucDauTuHMChuyenTiep);
designer.setDataSource("tongMucDauTuHangMuc", tongTongMucDauTuHMMoi + tongTongMucDauTuHMChuyenTiep);
designer.setDataSource("hangmucchuyentiep", cellsDataTableMapThongKeHangMucTheoNam_ChuyenTiep);
designer.process(false);
ByteArrayOutputStream ous = new ByteArrayOutputStream();
designer.getWorkbook().save(ous, SaveFormat.XLSX);
PortletResponse portletResponse = context.getPortletResponse();
HttpServletResponse res = PortalUtil.getHttpServletResponse(portletResponse);
res.setHeader("Content-Disposition", "attachment; filename=\"" + "thongkedanhmuchangmuc.xlsx" + "\"");
res.setHeader("Content-Transfer-Encoding", "binary");
res.setContentType("Content-type: application/vnd.ms-excel");
res.flushBuffer();
OutputStream out = res.getOutputStream();
byte[] buffer = new byte[4096];
int byteread;
InputStream is = new ByteArrayInputStream(ous.toByteArray());
while ((byteread = is.read(buffer)) != -1) {
out.write(buffer, 0, byteread);
buffer = new byte[4096];
}
out.flush();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
TRÊN VIEW
p:commandButton value="#{msgs['btn.indanhsach']}" rendered="#{thongKeDuAnBean.showInDanhSach}" ajax="false" icon="ui-icon-arrowthick-1-s">p:fileDownload value="#{thongKeDuAnBean.exportToExcel()}" /
/p:commandButton
Không có nhận xét nào:
Đăng nhận xét