overwrite existing table rows using DOCX4j - java

I am facing a problem in writing in MS Word file using Docx4j. The scenario is something like:
First user writes into the word file and supposedly he takes about 10 rows of the table and saves the file.
Second user comes and writes about 5 rows and saves the file again. But after sometime he again opens the file and modify something through java application.
Modified text is being added into the file instead of replacing the old text.
Now I want to replace his old text and want to save new paragraphs in table.
In simple words I want to replace old table rows with new one in Docx4j.
How can I reach to my desired rows to replace them?
Can anybody help me in this? Thanks

Use Replace to what you try do is just replace the String
Check this
https://www.docx4java.org/forums/docx-java-f6/replace-text-into-docx-within-a-table-t6.html

Related

Writing to a specific csv column

I have made a little javaFX app that reads from the database and writes to a file using openCSV.Everything is fine now.In the file i have a column and its values contain the path of an image(Example : /home/user/folder1/image.jpg).What i have to do is replace all the values of this column ,with another value that contains only the last folder name and the image name(Example : folder1/image.jpg).I read about this and i realised that i had to write a lot of code.What i am asking is if there is another way of doing this? For example can i replace all of this values directly from the query or can i use another library that lets me write to a specific column?
Thanks :)

How to read text file and write to Excel

I have a text file with several lines of data like this line:
04498203648;5067670000002534;15:44:50.987;15:44:51.533;546
04498203649;5067670000002344;15:44:50.988;15:44:51.534;546
I wish I could open the file, read each line and every semicolon separated value and insert them into a row in Excel, keeping one value per cell. Also I need to put some format into these cells, like keeping the format "[h]:mm:ss.000" for the timestamp values.
I can read the text file line by line, obtain each semicolon separated value, but I am a bit lost in putting them into the Excel file. Any easy way or tricks for handling this?
I am thinking of something like assigning each element of a line to 5 different variables and then writing these variables in line to Excel.
Java newbie here. Thanks in advance.

Change specific lines in a word file with Apache POI

I am using Apache POI lib in order to change automatically word files. I want to create a software which will have as an input some text, and put it in specific lines of the word file. How is it possible to have access in specific lines fo a word file? I mean how is it possible to create "variable areas" inside a word file.
You can use bookmarks inside Word documents using Microsoft Word. I have done it one week ago. I inserted bookmarks in the document by selecting the text that will be replaced then insert -> bookmark and type a new bookmark name. After that in your code you should implement something like this in your util classes :
Original code found here : http://apache-poi.1045710.n5.nabble.com/Replacing-the-value-of-the-bookmarks-td5710052.html

How to append existing line within a java text file

I'm having trouble adding to an exsisting line in a text file without overwriting that particular line or adding a new line.
for example, i have a line in my text file which is:
hello my name is
I would like to add to this line so it becomes:
hello my name is joe bloggs
Thanks
i have a task to create a help desk program and i am trying to incorporate a feature that enables users to edit questions they have posted. as a result, the program will need to be able to append Any line within the text file - not necessarily just the last line
If it's not at the end of the file, you're in trouble - you're basically talking about inserting data in the middle of a file, which isn't traditionally supported by file systems.
The normal way to approach this is to create a new file - copy the portion before the insertion point from the old file, then write your new data, then copy the remainder of the original file afterwards. Finally, do whatever renaming/deleting you need.

How to write data to a file through java?

I want to make a GUI application that contains three functions as follows:
Add a record
Edit a record
Delete a record
A record contains two fields - Name and Profession
There are two restrictions for the application
You can't use database to store info. You have to use a flat file.
Total file should not be re-written for every add/delete operation.
So, my questions are mentioned below:
Q1. Which file format would be better? (.xml or .csv or .txt or any other)
Q2. How can we perform the add/delete operation without the whole file being re-written?
The second part of your question is answered here : Best Way to Write Bytes in the Middle of a File in Java
As for the format - I would go with something as simple as possible. You don't want to have to deal with a bunch of markup processing, as using RandomAccessFile, you will going directly to a byte position. A fixed width style format would be good, so that based on the record number, you can calculate the starting position of a record or field in the file, without having to read everything in the file. The fields would then be padded out to the fixed width with spaces or some other suitable character.
I would go with CSV, zipped. it is both readable, and editable externally.
If CSV is your choice, this can help: http://javacsv.sourceforge.net/
Did you look at this? http://sourceforge.net/projects/flatworm/
Also consider Apache Derbi and HSQLDB
Another solution is this http://www.coyotegulch.com/products/jisp/index.html
You can reinvent the wheel, but that is only required if this is an academic assigment...
Given that the whole file must not be rewritten, I would suggest using RandomAccessFile that allow you to read and write only the record you want.
For the file format, a binary file, using fixed length for the record : ex: Name on 20 characters, Profession on 30.
This will allow you to use the seek() method of RandomAccessFile to directly access your data.

Categories