首页 > 其他分享 >excel

excel

时间:2023-01-18 15:15:35浏览次数:40  
标签:style String title indexOf excel ConvertUtil readLine

package com.enjoyit.pos.common.util;

import com.enjoyit.pos.common.CommonLog;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.util.List;

/**
 *  <dependency>
 *             <groupId>org.apache.poi</groupId>
 *             <artifactId>poi-ooxml-schemas</artifactId>
 *             <version>4.1.1</version>
 *         </dependency>
 *
 *         <dependency>
 *             <groupId>org.apache.commons</groupId>
 *             <artifactId>commons-collections4</artifactId>
 *             <version>4.1</version>
 *         </dependency>
 *         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
 *         <dependency>
 *             <groupId>org.apache.poi</groupId>
 *             <artifactId>poi-ooxml</artifactId>
 *             <version>4.1.1</version>
 *         </dependency>
 *         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
 *         <dependency>
 *             <groupId>org.apache.poi</groupId>
 *             <artifactId>poi</artifactId>
 *             <version>4.1.1</version>
 *         </dependency>
 */
public class PosExcelUtilXlsx {

    /**
     * 数据写入
     *
     * @param title    表格标题
     * @param colHead  列头(表头)
     * @param dataList 数据列表
     * @return 返回表格
     */
    public <T> XSSFWorkbook createExcel(String title, String[] colHead, List<Object[]> dataList) {
        // 创建一个表格
        XSSFWorkbook wb = new XSSFWorkbook();
        // 创建工作页并赋名
        XSSFSheet sheet = wb.createSheet("Sheet1");
        // 创建标题
        XSSFRow titleRow = sheet.createRow(0);
        XSSFCell titleCell = titleRow.createCell(0);
        // 设置标题高度
        titleRow.setHeight((short) (20 * 25));
        // 给标题设置样式
        XSSFCellStyle titleStyle = this.getTitleStyle(wb);
        // 给列头(表头)设置样式
        XSSFCellStyle colHeadStyle = this.getColHeadStyle(wb);
        // 给数据列表设置样式
        XSSFCellStyle dataListStyle = this.getDataListStyle(wb);
        // 合并单元格 起始行 截至列 起始列 截至行
//        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, dataList.get(0).length - 1));
        CellRangeAddress titleMerge = new CellRangeAddress(0, 0, 0, dataList.get(0).length - 1);
        sheet.addMergedRegion(titleMerge);
        RegionUtil.setBorderTop(BorderStyle.THIN, titleMerge, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, titleMerge, sheet);
        // 给标题样式附上
        titleCell.setCellStyle(titleStyle);
        // 给标题赋值
        titleCell.setCellValue(title);
        // 创建表头行
        XSSFRow colHeadRow = sheet.createRow(1);
        // 设置表头行高度
        colHeadRow.setHeight((short) (15 * 20));
        // 给表头设置样式并赋值
        for (int i = 0; i < colHead.length; i++) {
            XSSFCell colHeadCell = colHeadRow.createCell(i);
            colHeadCell.setCellStyle(colHeadStyle);
            colHeadCell.setCellValue(colHead[i]);
        }
        // 将查询到的数据赋到列表
        for (int i = 0; i < dataList.size(); i++) {
            // 获取每一行的元素
            Object[] data = dataList.get(i);
            // 创建行
            XSSFRow dataRow = sheet.createRow(i + 2);
            // 循环当前行的列元素设置样式并赋值
            for (int j = 0; j < data.length; j++) {
                XSSFCell dataRowCell = dataRow.createCell(j);
                dataRowCell.setCellStyle(dataListStyle);
                dataRowCell.setCellValue(data[j].toString());
                // 自适应列宽
                sheet.autoSizeColumn(j);
                sheet.setColumnWidth(j, sheet.getColumnWidth(j));
            }
        }
        return wb;
    }

    /**
     * 接口调用
     *
     * @param title    表格标题
     * @param colHead  列头(表头)
     * @param dataList 数据列表
     */
    public void exportExcel(String title, String[] colHead, List<Object[]> dataList, String fileName) {
        try {
            XSSFWorkbook result = createExcel(title, colHead, dataList);
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            result.write(fileOutputStream);
            fileOutputStream.close();
        } catch (Exception e) {
            CommonLog.getInstance().error(e);
        }
    }

    /**
     * 设置标题样式
     *
     * @param workbook 表格
     * @return 样式
     */
    public XSSFCellStyle getTitleStyle(XSSFWorkbook workbook) {
        // 设置字体
        XSSFFont font = workbook.createFont();
        // 设置字体大小
        font.setFontHeightInPoints((short) 15);
        // 设置字体加粗
        font.setBold(true);
        // 设置字体样式
        font.setFontName("Courier New");
        // 设置样式;
        XSSFCellStyle style = workbook.createCellStyle();
        // 在样式用应用设置的字体;
        style.setFont(font);
        // 设置自动换行;
        style.setWrapText(false);
        // 设置水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置垂直居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置单元格背景颜色
        style.setFillForegroundColor(IndexedColors.BLACK.getIndex());
        return style;
    }

    /**
     * 给列头设置样式
     *
     * @param workbook 表格
     * @return 样式
     */
    public XSSFCellStyle getColHeadStyle(XSSFWorkbook workbook) {
        // 设置字体
        XSSFFont font = workbook.createFont();
        // 设置字体大小
        font.setFontHeightInPoints((short) 12);
        // 设置字体样式
        font.setFontName("Courier New");
        // 设置字体加粗
        font.setBold(true);
        // 设置样式;
        XSSFCellStyle style = workbook.createCellStyle();
        // 在样式用应用设置的字体;
        style.setFont(font);
        // 设置水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置垂直居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 上边框
        style.setBorderTop(BorderStyle.THIN);
        // 下边框
        style.setBorderBottom(BorderStyle.THIN);
        // 左边框
        style.setBorderLeft(BorderStyle.THIN);
        // 右边框
        style.setBorderRight(BorderStyle.THIN);
        // 设置自动换行;
        style.setWrapText(false);
        // 设置单元格背景颜色
        style.setFillForegroundColor(IndexedColors.BLACK.getIndex());
        return style;
    }

    /**
     * 给数据列表设置样式
     *
     * @param workbook 表格
     * @return 样式
     */
    public XSSFCellStyle getDataListStyle(XSSFWorkbook workbook) {
        // 设置字体
        XSSFFont font = workbook.createFont();
        // 设置字体大小
        font.setFontHeightInPoints((short) 13);
        // 设置字体样式
        font.setFontName("Courier New");
        // 设置样式;
        XSSFCellStyle style = workbook.createCellStyle();
        // 在样式用应用设置的字体;
        style.setFont(font);
        // 设置水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置垂直居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 上边框
        style.setBorderTop(BorderStyle.THIN);
        // 下边框
        style.setBorderBottom(BorderStyle.THIN);
        // 左边框
        style.setBorderLeft(BorderStyle.THIN);
        // 右边框
        style.setBorderRight(BorderStyle.THIN);
        // 设置自动换行;
        style.setWrapText(false);
        // 设置单元格背景颜色
        style.setFillForegroundColor(IndexedColors.BLACK.getIndex());
        return style;
    }
}

  

public static void main(String[] args) {
        BufferedReader bufferedReader = PosFileUtils.readFile("D:\\1.txt");
        PosExcelUtilXlsx posExcelUtilXlsx = new PosExcelUtilXlsx();
        String[] title = new String[15];
        List<Object[]> contextList = new ArrayList<>();
        title[0] = "序号";
        title[1] = "日期";
        title[2] = "相差时间";
        title[3] = "年轻代总内存(M)";
        title[4] = "gc前年轻代用量(M)";
        title[5] = "gc后年轻代用量(M)";
        title[6] = "gc耗时";
        title[7] = "堆总内存(M)";
        title[8] = "gc前堆内存用量(M)";
        title[9] = "gc后堆内存用量(M)";
        title[10] = "gc耗时";
        title[11] = "总耗时";
        title[12] = "老年代总内存(M)";
        title[13] = "gc前老年代用量(M)";
        title[14] = "gc后老年代用量(M)";
        try {
            String readLine;
            String showDate = null;
            int in = 1;
            while (null != (readLine = bufferedReader.readLine())) {
                if (!readLine.contains("DefNew")) {
                    continue;
                }
                String[] context = new String[15];
                String date = readLine.substring(0, readLine.indexOf("+0800:")).replace("T", " ");
                String defNew = readLine.substring(readLine.indexOf("DefNew: ") + 8, readLine.indexOf("K->"));
                String gcDefNew = readLine.substring(readLine.indexOf("K->") + 3, readLine.indexOf("K("));
                String totalDefNew = readLine.substring(readLine.indexOf("K(") + 2, readLine.indexOf("K),"));
                String gcDefNewTime = readLine.substring(readLine.indexOf("), ") + 3, readLine.indexOf(" secs"));

                String heap = readLine.substring(readLine.indexOf("secs] ") + 6, readLine.indexOf("K->", readLine.indexOf("K->") + 1));
                String gcHeap = readLine.substring(readLine.indexOf("K->", readLine.indexOf("K->") + 1) + 3, readLine.indexOf("K(", readLine.indexOf("K(") + 1));
                String totalHeap = readLine.substring(readLine.indexOf("K(", readLine.indexOf("K(") + 1) + 2, readLine.indexOf("K),", readLine.indexOf("K),") + 1));
                String gcHeapTime = readLine.substring(readLine.indexOf("), ", readLine.indexOf("), ") + 1) + 3, readLine.indexOf(" secs", readLine.indexOf(" secs") + 1));
                String realContext = readLine.substring(readLine.indexOf("real=") + 5);
                String real = realContext.substring(0, realContext.indexOf(" secs"));
                String divDate = "0.00";
                String dateFormatter = DateFormatter.HYPHEN_YYYY_MM_DD_HH_MM_SS.getFormatter();
                SimpleDateFormat sdf = new SimpleDateFormat(dateFormatter);
                Date dateParse = sdf.parse(date);
                if (CommonMethod.isNotEmpty(showDate)) {
                    Date showDateParse = sdf.parse(showDate);
                    divDate = ConvertUtil.toString((dateParse.getTime() - showDateParse.getTime()) / 1000);
                }
                showDate = date;
                context[0] = ConvertUtil.toString(in);
                context[1] = date;
                context[2] = ConvertUtil.toString(divDate);
                context[3] = ConvertUtil.toString(PrecisionUtils.div(ConvertUtil.toDouble(totalDefNew), 1024));
                context[4] = ConvertUtil.toString(PrecisionUtils.div(ConvertUtil.toDouble(defNew), 1024));
                context[5] = ConvertUtil.toString(PrecisionUtils.div(ConvertUtil.toDouble(gcDefNew), 1024));
                context[6] = gcDefNewTime;
                context[7] = ConvertUtil.toString(PrecisionUtils.div(ConvertUtil.toDouble(totalHeap), 1024));
                context[8] = ConvertUtil.toString(PrecisionUtils.div(ConvertUtil.toDouble(heap), 1024));
                context[9] = ConvertUtil.toString(PrecisionUtils.div(ConvertUtil.toDouble(gcHeap), 1024));
                context[10] = gcHeapTime;
                context[11] = real;
                context[12] = ConvertUtil.toString(PrecisionUtils.div(PrecisionUtils.sub(ConvertUtil.toDouble(totalHeap), ConvertUtil.toDouble(totalDefNew)), 1024));
                context[13] = ConvertUtil.toString(PrecisionUtils.div(PrecisionUtils.sub(ConvertUtil.toDouble(heap), ConvertUtil.toDouble(defNew)), 1024));
                context[14] = ConvertUtil.toString(PrecisionUtils.div(PrecisionUtils.sub(ConvertUtil.toDouble(gcHeap), ConvertUtil.toDouble(gcDefNew)), 1024));
                in++;
                contextList.add(context);
            }
            posExcelUtilXlsx.exportExcel("GC数据分析表", title, contextList, "D:\\gc.xlsx");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

  

标签:style,String,title,indexOf,excel,ConvertUtil,readLine
From: https://www.cnblogs.com/ding08/p/17059834.html

相关文章

  • Python导入Excel表格数据并以字典dict格式保存
      本文介绍基于Python语言,将一个Excel表格文件中的数据导入到Python中,并将其通过字典格式来存储的方法。  我们以如下所示的一个表格(.xlsx格式)作为简单的示例。其中,表......
  • excel宏
    模版一Sub宏1()''宏1宏''Cells.SelectWithSelection.Font.Name="微软雅黑".Size=10.5.Strikethrough=False......
  • java,导出excel,控制数据垂直还是水平遍历,使用easyExcel
    java,导出excel,控制数据垂直还是水平遍历​​java导出excel,使用easyExcel,按指定格式导出,按模板导出,自定义表头导出​​这里的前提是,你用的easyExcel,就算你没有用,也可以试试......
  • excel的快捷键操作
    本章介绍excel中部分常用的快捷键1.文件类工作簿操作:Ctrl + N 新建;Ctrl + w 关闭当前;Ctrl + S 保存 F12 当前另存为  Ctrl + p 打印当前 2.通用......
  • 还在用 Excel 和 SQL?火山引擎 VeDI 这款产品帮你更快处理数据
    更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,并进入官方交流群对大多数职场打工人来说,看数据、用数据一直是项有“门槛”的工作。特别是在企业业务快速发展的......
  • python内存中生成excel和zip文件
    目录python内存中生成excel和zip文件直接返回Zip文件直接返回Excel文件内存中生成Excel文件再压缩生Zip文件python内存中生成excel和zip文件我们知道pandas和zipfile两个......
  • 还在用 Excel 和 SQL?火山引擎 VeDI 这款产品帮你更快处理数据
    更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,并进入官方交流群对大多数职场打工人来说,看数据、用数据一直是项有“门槛”的工作。特别是在企业业务快速......
  • 如何在EXCEL VBA中从DATE格式更改为数字?
    我将项目存储在字典中,其中键为date,例如'2012/05/11'。默认情况下,该键被改为数字(41040)。当我想要检索该项目时,我需要通过编号:dFRmonths.Item(41040)来引用它。由于dFRmonths......
  • 第一章 excel与数据格式
    part1数据缘何而来excel中常见的文件格式有xls与xlsx,推荐后者(空间小、容量大、速度快等特点)单个excel文件为工作簿,其下包含工作表sheet(最多255),sheet中的每个格子为......
  • vue项目导出功能_前端导出Excel_Export2Excel.js
    VUE项目导出功能_前端导出Excel_Export2Excel.jsvue项目利用插件直接进行excel生成。1.安装依赖[email protected]......