Search in sources :

Example 81 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project financial by greatkendy123.

the class ExportExcelTemplate method export.

/*
     * 导出数据
     * */
public void export() throws Exception {
    try {
        // 创建工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet(title);
        // // 产生表格标题行
        // HSSFRow rowm = sheet.createRow(0);
        // HSSFCell cellTiltle = rowm.createCell(0);
        // 
        // //            sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面  - 可扩展
        // 获取列头样式对象
        HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);
        // 单元格样式对象
        HSSFCellStyle style = this.getStyle(workbook);
        // 
        // sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (rowName.length-1)));
        // cellTiltle.setCellStyle(columnTopStyle);
        // cellTiltle.setCellValue(title);
        /**
         ***********************************  正文  ***************************
         */
        // 定义所需列数
        int columnNum = rowName.length;
        // 在索引2的位置创建行(最顶端的行开始的第二行)
        HSSFRow rowRowName = sheet.createRow(1);
        /**
         ***********************************  标题栏  ***************************
         */
        for (int n = 0; n < columnNum; n++) {
            // 创建列头对应个数的单元格
            HSSFCell cellRowName = rowRowName.createCell(n);
            // 设置列头单元格的数据类型
            cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);
            HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
            // 设置列头单元格的值
            cellRowName.setCellValue(text);
            // 设置列头单元格样式
            cellRowName.setCellStyle(columnTopStyle);
        }
        // 将查询出的数据设置到sheet对应的单元格中
        for (int i = 0; i < dataList.size(); i++) {
            // 遍历每个对象
            Object[] obj = dataList.get(i);
            // 创建所需的行数
            HSSFRow row = sheet.createRow(i + 2);
            // 设置单元格的数据类型
            HSSFCell cell = null;
            for (int j = 0; j < obj.length; j++) {
                cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);
                if (!"".equals(obj[j]) && obj[j] != null) {
                    // 设置单元格的值
                    cell.setCellValue(obj[j].toString());
                }
                // 设置单元格样式
                cell.setCellStyle(style);
            }
        }
        // 让列宽随着导出的列长自动适应
        for (int colNum = 0; colNum < columnNum; colNum++) {
            int columnWidth = sheet.getColumnWidth(colNum) / 256;
            for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
                HSSFRow currentRow;
                // 当前行未被使用过
                if (sheet.getRow(rowNum) == null) {
                    currentRow = sheet.createRow(rowNum);
                } else {
                    currentRow = sheet.getRow(rowNum);
                    currentRow.setHeight((short) 400);
                }
            }
            // if(colNum == 0){
            // sheet.setColumnWidth(colNum, (columnWidth+6) * 256);
            // }else{
            // sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
            // }
            sheet.setColumnWidth(colNum, columnWidths.get(colNum));
        }
        if (workbook != null) {
            OutputStream out = null;
            try {
                File file = new File(this.out + title + ".xls");
                out = new FileOutputStream(file);
                workbook.write(out);
                java.awt.Desktop.getDesktop().open(file);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (out != null)
                    out.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) IOException(java.io.IOException) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) IOException(java.io.IOException) HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) FileOutputStream(java.io.FileOutputStream) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) File(java.io.File)

Example 82 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project financial by greatkendy123.

the class ExportTGExcel method setWaizhaiData.

/**
 * 设置外债的Excel数据
 * @time 2018年3月20日
 * @param workbook
 * @param excelModel
 */
private void setWaizhaiData(HSSFWorkbook workbook, TGExcelModel excelModel) {
    HSSFSheet sheet = workbook.createSheet(excelModel.getSheetName());
    Map<String, List<Object[]>> waizhaiMap = excelModel.getWaizhaiMap();
    // 获取列头样式对象
    HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);
    // 单元格样式对象
    HSSFCellStyle style = this.getStyle(workbook);
    // 第几个表
    int index = 1;
    for (Map.Entry<String, List<Object[]>> entry : waizhaiMap.entrySet()) {
        HSSFRow r = sheet.getRow(1);
        if (r == null)
            r = sheet.createRow(1);
        int start = (index - 1) * 3;
        int end = (index - 1) * 3 + 1;
        // 标题
        String teamString = entry.getKey();
        HSSFCell cellName = r.createCell((index - 1) * 3);
        cellName.setCellType(HSSFCell.CELL_TYPE_STRING);
        HSSFRichTextString text = new HSSFRichTextString(teamString.split("#")[0]);
        cellName.setCellValue(text);
        cellName.setCellStyle(columnTopStyle);
        cellName = r.createCell((index - 1) * 3 + 1);
        cellName.setCellType(HSSFCell.CELL_TYPE_STRING);
        text = new HSSFRichTextString(teamString.split("#")[1]);
        cellName.setCellValue(text);
        cellName.setCellStyle(columnTopStyle);
        // 外债明细
        int i = 1;
        List<Object[]> waizhaiList = entry.getValue();
        for (Object[] obj : waizhaiList) {
            HSSFRow row = sheet.getRow(i + 1);
            if (row == null) {
                row = sheet.createRow(i + 1);
                row.setHeight((short) 400);
            }
            HSSFCell type = row.createCell(start, HSSFCell.CELL_TYPE_STRING);
            type.setCellStyle(style);
            type.setCellValue(obj[0].toString());
            HSSFCell sum = row.createCell(start + 1, HSSFCell.CELL_TYPE_STRING);
            sum.setCellStyle(style);
            sum.setCellValue(obj[1].toString());
            sheet.setColumnWidth((i - 1) * 3, 3500);
            sheet.setColumnWidth((i - 1) * 3 + 1, 3500);
            i += 1;
        }
        index += 1;
    }
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 83 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project bitcampSCOpen2017 by ryuyj.

the class MemberListXlsView method buildExcelDocument.

@SuppressWarnings("unchecked")
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
    HSSFSheet sheet = createFirstSheet(workbook);
    createColumnLabel(sheet);
    // 컨트롤러에서 전송한 memberList를 받아옴
    List<Member> memberList = (List<Member>) model.get("memberList");
    int rowNum = 1;
    for (Member member : memberList) {
        createMemberListRow(sheet, member, rowNum++);
    }
    String fileName = "memberList.xls";
    response.setContentType("Application/Msexcel");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
}
Also used : HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) List(java.util.List) Member(com.bitcamp.open.member.model.Member)

Example 84 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project bitcampSCOpen2017 by ryuyj.

the class PageRanksView method buildExcelDocument.

@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 1. 시트 생성
    HSSFSheet sheet = createFirstSheet(workbook);
    // 2. 라벨 처리
    createColumnLabel(sheet);
    // 3. 각 행별 데이터 처리
    List<Member> list = (List<Member>) model.get("member");
    int rowNum = 1;
    for (Member e : list) {
        createPageRankRow(e, sheet, rowNum++);
    }
    // 파일 이름설정
    response.setHeader("Content-Disposition", "attachment; filename=\"Member.xls\";");
}
Also used : HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) List(java.util.List) Member(com.bitcamp.open.member.Model.Member)

Example 85 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project bitcampSCOpen2017 by ryuyj.

the class PageRanksView method createFirstSheet.

// 시트를 생성해주는 메서드
private HSSFSheet createFirstSheet(HSSFWorkbook workbook) {
    // 시트생성
    HSSFSheet sheet = workbook.createSheet();
    // 생성된 시트 이름 설정
    workbook.setSheetName(0, "페이지 순위");
    // 시트 컬럼의 사이즈 지정
    sheet.setColumnWidth(1, 256 * 20);
    return sheet;
}
Also used : HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet)

Aggregations

HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)213 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)172 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)97 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)84 Test (org.junit.Test)67 FileOutputStream (java.io.FileOutputStream)34 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)27 IOException (java.io.IOException)25 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)25 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)25 File (java.io.File)21 ArrayList (java.util.ArrayList)20 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)20 Row (org.apache.poi.ss.usermodel.Row)18 FileInputStream (java.io.FileInputStream)17 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)15 OutputStream (java.io.OutputStream)14 HashMap (java.util.HashMap)14 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)13 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)12