JAVA-Create Excel Write List<HashMap<K, V>>to Excel Convert to excel Read local file excel to byte stream string

Blog background: JAVA project, read the database content, create Excel and save it to the specified location~

1. Create Excel

1、XSSFWorkbook

Create a job page;

2、XSSFSheet

Create a table and set the row width, height, font, etc.;

3. Set storage location

positon is the file storage location, and you can directly set the location to the specified location on the disk; as follows, the default is the upper-level directory where the project runs;

4. Required data conversion

Maps are List<HashMap<String, Object>> type data taken from the database;

5. Data assignment

Read maps and assign values ​​in a loop;

6、File、 FileOutputStream

Create excel files using File and FileOutputStream

code show as below:

public void createExcel() {
    
    
        String positon = "../test.xlsx";
        List<HashMap<String, Object>> maps = testMapper.getTestInfo();
        // 定义一个新的工作簿
        XSSFWorkbook wb = new XSSFWorkbook();
        // 创建一个Sheet页,命名为first
        XSSFSheet sheet = wb.createSheet("first");
        //设置行高
        sheet.setDefaultRowHeight((short) (2 * 256));
        //设置列宽
        sheet.setColumnWidth(0, 4000);
        sheet.setColumnWidth(1, 4000);
        sheet.setColumnWidth(2, 4000);
        XSSFFont font = wb.createFont();
        //设置字体
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);
        //获得表格第一行
        XSSFRow row = sheet.createRow(0);
        //根据需要给第一行每一列设置标题
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("Name");
        cell = row.createCell(1);
        cell.setCellValue("Sex");
        cell = row.createCell(2);
        cell.setCellValue("Age");
        cell = row.createCell(3);
        XSSFRow rows;
        XSSFCell cells;

        //循环拿到的数据给所有行每一列设置对应的值
        for (int i = 0; i < maps.size(); i++) {
    
    
            // 在这个sheet页里创建一行
            rows = sheet.createRow(i + 1);n
            HashMap<String, Object> map = maps.get(i);
            // 该行创建一个单元格,在该单元格里设置值
            String name = map.get("name").toString();
            String sex = map.get("sex").toString();
            Long age = 0L;
            cells = rows.createCell(0);
            cells.setCellValue(taskName);
            cells = rows.createCell(1);
            cells.setCellValue(domain);
            cells = rows.createCell(2);
            cells.setCellValue(age);
        }
        try {
    
    
        	//创建文件的路径
            File file = new File(positon);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            //将创建的表格写入
            wb.write(fileOutputStream);
            wb.close();
            fileOutputStream.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

2. Read files

Read the local file, convert it into a byte stream, and then convert it into string format to output
the positon as the file storage location. You can directly set the location to the specified location on the disk; the following defaults to the upper-level directory where the project is running;

code show as below:

String positon =  "../test.xlsx";
//读取文件
InputStream is = new FileInputStream(positon);
int iAvail = is.available();
//转为字节流
byte[] bytes = new byte[iAvail];
is.read(bytes);
//转成string
String table = new String(bytes);

Reference:
https://www.cnblogs.com/mythz/p/14177739.html
https://blog.csdn.net/qq_39898191/article/details/104500896

Guess you like

Origin blog.csdn.net/weixin_44436677/article/details/128228737