欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 如何將表格數(shù)據(jù)導(dǎo)入word文檔中

 更新時間:2021年06月18日 15:15:24   作者:George.Liu73  
這篇文章主要介紹了Java將表格數(shù)據(jù)導(dǎo)入word文檔中的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Java 表格數(shù)據(jù)導(dǎo)入word文檔中

個人覺得這個功能實在搞笑,沒什么意義,沒辦法提了需求就要實現(xiàn),(太好說話了把我)

我的實現(xiàn)是再word中生成一個與 excel行,列 一樣的一個表格,然后把從excel拿到的數(shù)據(jù)(exList參數(shù))依次放到word表格中

    public static void createFile(HttpServletResponse response, String fileName, List<List<String>> exList) {
  
        try {
            setResponseHeader(response, fileName);
            //生成一個word模版文件
            XWPFDocument document = new XWPFDocument();
 
            XWPFTable table = document.createTable(exList.size(), exList.get(0).size());
            XWPFTableRow row;
 
            for (int i = 0; i < exList.size(); i++) {
 
                List<String> sdf = exList.get(i);
 
                row = table.getRow(i);
 
                for (int j = 0; j < exList.get(i).size(); j++) {
                    String s =sdf.get(j);
                    row.getCell(j).setText(s);
                    row.getCell(j).setWidthType(TableWidthType.AUTO);
                }
                //將數(shù)據(jù)插入表格中  pos:0 表示 第一個表格
                document.setTable(0,table);
            }
            ServletOutputStream outputStream = response.getOutputStream();
            BufferedOutputStream bufferStream = new BufferedOutputStream(outputStream, 1024);
            document.write(bufferStream);
            document.close();
            bufferStream.close();;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void setResponseHeader(HttpServletResponse response, String name) {
        try {
            name = new String(name.getBytes(), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
 response.setContentType("multipart/form-data");
        //要保存的文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + name + ".docx");
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
    }

Java poi導(dǎo)入word表格數(shù)據(jù)的經(jīng)過

一、過程及遇到的問題和解決思路

需要導(dǎo)入的是一個word文檔,內(nèi)容是以表格的形式保存在word中

1、poi對word表格的空格處可以自動識別出來并賦值為 " ",這一點比poi導(dǎo)入excel人性化(excel默認是跳過這個空格)

2、對于某些情況下,肉眼無法看出表格格式問題,但是程序可以識別出來,懷疑是表格后期人工修改過,導(dǎo)致表格外觀沒問題但是行列屬性不一致,導(dǎo)致讀取時遇到這些地方報錯,解決思路:可以在讀取每一行之前先判斷列數(shù)是否正確,poi中可以獲取每行的列數(shù),不正確的證明此列有問題,舍棄跳過。

二、代碼

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.17</version>
        </dependency>
package com.example.importtomysql; 
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*; 
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; 
public class ImportWord {
    public List<TableColumn> testReadByDoc(String path) throws Exception {
        File f = new File(path);
        InputStream is = new FileInputStream(f);
        HWPFDocument doc = new HWPFDocument(is);
        //輸出書簽信息
        //  this.printInfo(doc.getBookmarks());
        //輸出文本
        //  System.out.println(doc.getDocumentText());
        Range range = doc.getRange();
        //   this.printInfo(range);
        //讀表格
        List<TableColumn> tableColumns = this.readTable(range);
        //讀列表
        //  this.readList(range);
        //把當前HWPFDocument寫到輸出流中
        // doc.write(new FileOutputStream("D:\\temp\\test.doc"));
        is.close();
        return tableColumns;
    }
 
    /**
     * 輸出書簽信息
     * @param bookmarks
     */
    private void printInfo(Bookmarks bookmarks) {
        int count = bookmarks.getBookmarksCount();
        System.out.println("書簽數(shù)量:" + count);
        Bookmark bookmark;
        for (int i=0; i<count; i++) {
            bookmark = bookmarks.getBookmark(i);
            System.out.println("書簽" + (i+1) + "的名稱是:" + bookmark.getName());
            System.out.println("開始位置:" + bookmark.getStart());
            System.out.println("結(jié)束位置:" + bookmark.getEnd());
        }
    }
 
    /**
     * 讀表格
     * 每一個回車符代表一個段落,所以對于表格而言,每一個單元格至少包含一個段落,每行結(jié)束都是一個段落。
     * @param range
     */
    private List<TableColumn> readTable(Range range) {
        List<TableColumn> tableColumns = new ArrayList<>();
        //遍歷range范圍內(nèi)的table。
        TableIterator tableIter = new TableIterator(range);
        Table table;
        TableRow row;
        TableCell cell;
        int i=0;
        int k=0;
        while (tableIter.hasNext()&&i<=1) {
 
            table = tableIter.next();
            int rowNum = table.numRows();
            for (int j=0; j<rowNum; j++) {
                TableColumn tableColumn = new TableColumn();
                row = table.getRow(j);
                int cellNum = row.numCells();
//                for (int k=0; k<cellNum; k++) {
//                    cell = row.getCell(k, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
//
//                    //輸出單元格的文本
//                    System.out.println(cell.text().trim());
//                }
                k++;
                if(12==cellNum){
                    tableColumn.setId(row.getCell(0).text().trim());
                    tableColumn.setSscj(row.getCell(1).text().trim());
                    tableColumn.setQlfl(row.getCell(2).text().trim());
                    tableColumn.setXmmc(row.getCell(3).text().trim());
                    tableColumn.setZx(row.getCell(4).text().trim());
                    tableColumn.setBlx(row.getCell(5).text().trim());
                    tableColumn.setSsyj(row.getCell(6).text().trim());
                    tableColumn.setCbjg(row.getCell(7).text().trim());
                    tableColumn.setZrsx(row.getCell(8).text().trim());
                    tableColumn.setSxyj(row.getCell(9).text().trim());
                    tableColumn.setZzqx(row.getCell(10).text().trim());
                    tableColumn.setZzyj(row.getCell(11).text().trim());
//                tableColumn.setBz(row.getCell(12).text().trim());
                    tableColumns.add(tableColumn);
  
                    if(679==k){
                        System.out.println(k  +" " +row.getCell(0).text().trim()+" " +row.getCell(3).text().trim());
                    }
//                    System.out.println(k +" " +row.getCell(0).text().trim()+" "+row.getCell(3).text().trim());
                }else {
                    System.out.println(k);
                }
 
            }
            i++;
        }
        return tableColumns;
    }
 
    /**
     * 讀列表
     * @param range
     */
    private void readList(Range range) {
        int num = range.numParagraphs();
        Paragraph para;
        for (int i=0; i<num; i++) {
            para = range.getParagraph(i);
            if (para.isInList()) {
                System.out.println("list: " + para.text());
            }
        }
    }
 
    /**
     * 輸出Range
     * @param range
     */
    private void printInfo(Range range) {
        //獲取段落數(shù)
        int paraNum = range.numParagraphs();
        System.out.println(paraNum);
        for (int i=0; i<paraNum; i++) {
            System.out.println("段落" + (i+1) + ":" + range.getParagraph(i).text());
        }
        int secNum = range.numSections();
        System.out.println(secNum);
        Section section;
        for (int i=0; i<secNum; i++) {
            section = range.getSection(i);
            System.out.println(section.getMarginLeft());
            System.out.println(section.getMarginRight());
            System.out.println(section.getMarginTop());
            System.out.println(section.getMarginBottom());
            System.out.println(section.getPageHeight());
            System.out.println(section.text());
        }
    }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論