开发者

With grails How to create a doc file having table with apache POI and write data in it?

开发者 https://www.devze.com 2023-03-22 12:56 出处:网络
In my grails application i need to urgently create a file in current system in which i need to save开发者_StackOverflow information fetched from table in database.

In my grails application i need to urgently create a file in current system in which i need to save开发者_StackOverflow information fetched from table in database.

I have created file as,

File file=new File("file name.txt")

file.createNewFile();

then i have wrote values of mysql db table fields in it as,

file<<patient.id
file<<patient.name
.
.
.

it stores data like continous text but i want to have a .doc file in which data should get stored in table.On googling i found APACHE'S POI for creating doc file but i am not getting how it works and how i should use it.

With advance thanks, Laxmi.p


It is complicated to the point of it not being worth any persons effort to make tables (that do not randomly break) in HWPF. If you can use docx then the XWPF packages are easy to achieve this with.

import org.apache.poi.xwpf.*

class DocumentUtil {

    public static XWPFDocument makeDocument(metaData, tableData) {
        //make document and apply metadata, like regular text, headings bla bla
        def doc = new XWPFDocument()
        def table = doc.createTable(metaData.rows, metaData.cols)
        table = popuLateTable(table, tableData) //implement this part yourself

        return doc
    }
}

If you cant use docx, I'd reccomend going for an excel document(HSSFWorkbook, even easier) or just a simple rtf (which is basically text with annotations for styling).

Edit: Which I see has been mentioned in a comment further up here.

0

精彩评论

暂无评论...
验证码 换一张
取 消