Problem while copying JTable contents into Excel - java

I have a Standalone application in which I am trying to copy contents of JTable into Excel with line breaks included in my cells of JTable. I have used wrapping using "\"" to my cell contents. It's working fine but I am getting a square box type of symbol in place of line breaks in Excel. How to remove the symbol while copying? Is there any way to do this?

Excel expects DOS/Windows line breaks ("\r\n") instead of Unix/Java ones (just "\n"). To fix this, use this code:
s = s.replace("\r\n", "\n").replace("\n", "\r\n");
The first part makes sure that you don't have any DOS/Win line breaks and then converts everything to DOS.
If you're stuck with Java 1.4, use replaceAll().
[EDIT] The square box means that there is an unprintable character in the string. I suggest to create a file with Excel which contains a line break in a cell and check which characters Excel uses.

Related

Input the file and count the characters

I am writing a code that calculates how many words in a file
And my problem is: when I input the file, there will have more word than the original file...For example, in the file, the content is abcdabcd, but when I run the code, the console shows Total no. of letters: 194
I am using netbeans IDE and mac, when I click the blank space instead of directly open the file, I found there are many words in front of abcdabcd, I guess perhaps this is the reason... But I don't know how to fix this problem on my code
[![enter image description here][1]][1]
Can anyone help me solve this problem?
Thanks!
The input file is encoded with rtf (Rich Text Format), despite it being saved with a .txt extension. This encoding is commonly used to do many things plain text cannot do, such as bold and italic etc. However, to accomplish this, the file is filled with all sorts of other things to accompany the normal text, in your case "abcdef". Of course, java reads all of this as if it were a plain text file, and ends up counting all of the rtf formatting as well.
I assume you're using TextEdit, so look at this tutorial to see how to use only plain text, so all of the extra formatting is not included in the final file.
Hope this helped :)

Petaho kettle Java expression to split cell and duplicate value in excel horizontally

I am using Kettle for data migration for moving data, I come across a scenario where i got a excel where the column header is merged, When i read it only the first cell gets the name others are empty, Kettle can only duplicate values vertically, I have a macro which does the exact same thing, But some one has to do it manually, Is there a java code i could run to achieve this?
What do you mean by merged? In the excel file is there one cell with all the values? Is there a separator? If it's not a great deal of columns I'd just manually add them or change them to what I want using a select step. If you'd like to upload files I'll take a look.
I think, got the solution. Here Java code is not required. Please follow the step to achieve your result.
Step1: Drag and drop Microsoft Excel Input to the canvas.And go to Content uncheck Header.
Step2: Go to !Fields tab. See the below image and fill up like below image.
Step3:Go to Additional output fields tab and then check for sheet row nr field and fill up the check box with row_number.
Step4:Finally, click on preview button. You get the result like below.
If you see the result. First three rows are unnecessary. You can filter those three rows by using Filter step this part I'am not done.

convert a method to work with gui

I've got a huge method that prints multiple lines, numbers, characters and uses system.out and multiple data types, it works. But I'd like to use it in a jframe. I tried converting every system.out statement to a jtextArea.setText(), and did casting for non string types but nothing comes out when I run it.
Is it possible? what is the right way of doing that.
jtextarea right for my method.
If you wish to append text to a JTextArea, use the append method. Right now your code is using setText, which does just that: sets the text of the JTextArea, removing all previous text in the process (and in this way your code seems to almost guarantee that the JTextArea either contain no text, or contain a single new line character).
try jTextField if its only gonna output.
Try jTextField.append if the user will write stuff on the text.

Changing text font color when writing to a text file through printstream Java

I use a printstream to write the contents of an array list to a text file. An element of the list would look something like this:
Name: 2D19 Fingerprint: 170 Identity: Loop
The way I write it is through a for loop
ArrayList<String> array = new ArrayList<String>();
for(int i=0; i<array.size(); array++){
ps.println(array.get(i));
}
I want to know if it is possible to change the color of any of the text I write. I tried researching this but all the examples I've seen involve writing in a JTextArea, which I do not want to do.
It depends on the 'media' where you are printing at. The question is not very clear on this: you are saying you are writing strings to a text file, hence you have no colors there.
But you may print your strings in the HTML file and use HTML syntax to markup the written text, so when you preview such file, the text will be colored.
Or if you are printing to console, you may use ANSI escape codes for color, which works out of box on linux.

Without having any form of GUI, what set of classes should be used to represent formatted text in memory?

I have an application I'm writing which reads a docx file. It appears that I may need to read the formatting of the text, and not just the content. I have googled the matter but finding a search term that finds me what I'm looking for, most of it points me to using formatted text inputs and the like.
Does anyone know what class I should be using?
Apache poi should give you (at least some) access to Excel styles - such as colors, fonts etc. I'm not sure about exotic cases, but it's certainly possible to obtain cell font color for example. The following code works for me:
XSSFCell cell = ...
if (IndexedColors.WHITE.getIndex() == cell.getCellStyle().getFont().getColor()) {
...
}

Categories

Resources