Search in sources :

Example 51 with HSSFCell

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

the class ExportQuotaPayExcel 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);
        /**
         *********************************** 正文 ***************************
         */
        // 定义所需列数
        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 || colNum == 1) {
                sheet.setColumnWidth(colNum, (columnWidth + 4) * 500);
            } else if (colNum == 5) {
                sheet.setColumnWidth(colNum, (columnWidth + 6) * 500);
            } else if (colNum == 6) {
                sheet.setColumnWidth(colNum, (columnWidth + 10) * 500);
            } else {
                sheet.setColumnWidth(colNum, (columnWidth + 1) * 400);
            }
        }
        if (workbook != null) {
            OutputStream out = null;
            try {
                File file = new File(this.out + ".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 52 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell 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 53 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell 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 54 with HSSFCell

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

the class MemberListXlsView method createColumnLabel.

private void createColumnLabel(HSSFSheet sheet) {
    HSSFRow firstRow = sheet.createRow(0);
    HSSFCell cell = firstRow.createCell(0);
    cell.setCellValue("회원번호");
    cell = firstRow.createCell(1);
    cell.setCellValue("이메일");
    cell = firstRow.createCell(2);
    cell.setCellValue("이름");
    cell = firstRow.createCell(3);
    cell.setCellValue("가입일");
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow)

Example 55 with HSSFCell

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

the class MemberListXlsView method createColumnLabel.

private void createColumnLabel(HSSFSheet sheet) {
    HSSFRow firstRow = sheet.createRow(0);
    HSSFCell cell = firstRow.createCell(0);
    cell.setCellValue("회원번호");
    cell = firstRow.createCell(1);
    cell.setCellValue("이름");
    cell = firstRow.createCell(2);
    cell.setCellValue("아이디");
    cell = firstRow.createCell(3);
    cell.setCellValue("가입일");
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow)

Aggregations

HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)147 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)96 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)93 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)91 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)31 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)30 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)28 Test (org.junit.Test)25 FileOutputStream (java.io.FileOutputStream)24 IOException (java.io.IOException)18 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)16 File (java.io.File)13 CellValue (org.apache.poi.ss.usermodel.CellValue)13 ArrayList (java.util.ArrayList)11 AssertionFailedError (junit.framework.AssertionFailedError)10 HashMap (java.util.HashMap)9 OutputStream (java.io.OutputStream)8 Map (java.util.Map)8 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)8 FileInputStream (java.io.FileInputStream)6