How to write a carriage return into xls file in Java - java

I would like to insert a carriage return into a cell of a xls file.
So I have written this code
address = rs.getString(16) + " " + rs.getString(17) + "
"
+ rs.getString(18) + " " + rs.getString(19) + " (" +
rs.getString(20) + ")";
"writer.write("<ss:Cell><ss:Data ss:Type=\"String\">" + address + "</ss:Data></ss:Cell>");`
but in the Excel File the result is that the carriage return is replaced with a "square symbol". In which mode can I resolve this issue?
Thanks,
Stefano

In excel, to enter a new line in a cell, you need to insert ASCII characters 13 + 10 (constant CrLf on this page : http://msdn.microsoft.com/en-us/library/f63200h0%28v=vs.80%29.aspx).
Have you tried:
String crLf = Character.toString((char)13) + Character.toString((char)10);
address = rs.getString(16) + " " + rs.getString(17) + crLf
+ rs.getString(18) + " " + rs.getString(19) + " (" +
rs.getString(20) + ")";

Related

how to simulate Excel control codes in String

One web project I'm involved in requires generating a CSV to the user.
One field is notes and there can be multiple notes in the field, separated by a return (alt-enter in Excel), appearing as
A USER entered on 02/17/2023: Test note
A USER entered on 02/17/2023: This is another note
Is it possible to insert control codes into a Java string that Excel will pick up and format the cell properly?
For what it's worth, this was my attempt:
}else if (formType.equalsIgnoreCase("downloadCSV")) {
Date d = new Date();
String filename = "C:/temp/rtTmp" + d.getTime() + ".csv";
File f = new File(filename);
List<Contact> contactList = RetentionTrackDatabaseUtil.getContacts();
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write("name,"
+ "gender,"
+ "age,"
+ "DOB,"
+ "DOH,"
+ "DOR,"
+ "DOS,"
+ "dateContact,"
+ "reason,"
+ "status,"
+ "empNum,"
+ "shift,"
+ "phone,note(s)\n");
for (Contact c : contactList) {
System.out.println(c.getAge());
bw.append("\"" + c.getName() + "\"" + ","
+ c.getGender() + ","
+ Integer.toString(c.getAge()) + ","
+ (c.getDOB()!=null?RetentionTrackUtil.sdf.format(c.getDOB()):"") + ","
+ (c.getDOH()!=null?RetentionTrackUtil.sdf.format(c.getDOH()):"") + ","
+ (c.getDOR()!=null?RetentionTrackUtil.sdf.format(c.getDOR()):"") + ","
+ (c.getDOS()!=null?RetentionTrackUtil.sdf.format(c.getDOS()):"") + ","
+ (c.getDateContact()!=null?RetentionTrackUtil.sdf.format(c.getDateContact()):"") + ","
+ c.getReason() + ","
+ c.getStatus() + ","
+ c.getEmpNum() + ","
+ c.getShift() + ","
+ c.getPhone() + ",");
for(Note n: c.getNoteList()){
bw.append("(" + n.getEnteredBy() + " on " + RetentionTrackUtil.sdf.format(n.getNoteDate()) + ")" + n.getNote() + "\\n");
}
bw.append("\n");
}
bw.close();
sendFile(response, filename);
}

Formatting a return string

I am trying to format this return string to display ratings at one decimal place.
return String.format(title + "," + genre + "," + releaseYear + "(" + (getRating() == -1 ? "No ratings" : getRating()) + "): " + numOfDeaths + " deaths");
I keep getting an error saying too few parameters passed.
You want java.text.DecimalFormat.
DecimalFormat df = new DecimalFormat("0.0##");
String result = df.format(getRating());
return String.format(title + "," + genre + "," + releaseYear + "(" + (getRating() == -1 ? "No ratings" : result) + "): " + numOfDeaths + " deaths");

sending Meeting request outlook in java , and linebreaks in "DESCRIPTION:" are takenout

String textEmail= "Hi, \n this is an automatic message \n from sender.
StringBuffer buffer = sb.append("BEGIN:VCALENDAR\n" +
"PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n" +
"VERSION:2.0\n" +
"METHOD:REQUEST\n" +
"BEGIN:VEVENT\n" +
"ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:" +emailAdressTo+"\n" +
"ORGANIZER:MAILTO:XX#gmail.com\n" +
"DTSTART:" + meetingStartTime + "\n"+
"DTEND:" + meetingEndTime + "\n"+
"LOCATION: Room FR PAR-New York\n" +
"TRANSP:OPAQUE\n" +
"SEQUENCE:0\n" +
"UID:"+ uniqueId +"\n" +
"DTSTAMP:"+ meetingEndTime + "\n" +
"CATEGORIES:Meeting\n" +
"DESCRIPTION:"+textEmail +"\n" +
"SUMMARY:"+subjectEmail+"\n" +
"PRIORITY:5\n" +
"CLASS:PUBLIC\n" +
"BEGIN:VALARM\n" +
"TRIGGER:PT1440M\n" +
"ACTION:DISPLAY\n" +
"DESCRIPTION:Reminder\n" +
"END:VALARM\n" +
"END:VEVENT\n" +
"END:VCALENDAR");
email.setContent(buffer.toString(),"text/calendar");
email.setCharset("UTF-8");
the linebreaks breaks are not taken in consideration and my output is just:
"Hi,this is an automatic message from sender"
I have tried every think in this post:
[1]How do I format a String in an email so Outlook will print the line breaks? but still same result Any help would be apreciated, thks
List item

HTML in Android seems not working

I want customize a string with results that will be written in a TextView but not works. I just want color some part of text using html tags but it's still all text with same color. This is what i wrote so far:
#Override
public String toString() {
return (Html.fromHtml("<font color=\"#e61624\">"+fromHour+"</font>")) + " " + from + " / " + toHour + " " + to;
}
Thanks
I wouldn't use Html.fromHtml inside the toString() method.
In toString(), simply return the corresponding text:
return "<font color=\"#e61624\">"+fromHour+"</font> " + from + " / " + toHour + " " + to;
Then call:
textView.setText(Html.fromHtml(yourObject.toString());
// Try this way,hope this will help you to solve your problem.
yourTextView.setText(Html.fromHtml("<font color=\"#e61624\">"+fromHour+"</font> " + from + " / " + toHour + " " + to));
String text = "<font color=\"#e61624\">"+"<small>" + "Hello"
+ "</small>"+"</font>";
Html.fromHtml(text);

Java, Android - Handle comma in csv export

I am exporting a table to a .csv file, but I have the usual problem with commas in the fields.
This is how I populate the .csv file:
outCSV.write(todoItemsID.get(a) + "," + todoItemsGROUP.get(a) + "," + todoItemsNAME.get(a) + ","
+ todoItemsFINISHED.get(a) + "," + todoItemsNOTES.get(a) + "," + todoItemsQUANTITY.get(a) + "\n");
What if there is a comma or a new line e.g. in the todoItemsNotes.get(a) field? How should I handle that?
Use double-quotes to surround that string. It generally works for most tools that read CSV files.
outCSV.write(todoItemsID.get(a) + "," + todoItemsGROUP.get(a) + "," + todoItemsNAME.get(a) + ","
+ todoItemsFINISHED.get(a) + ",\"" + todoItemsNOTES.get(a) + "\"," + todoItemsQUANTITY.get(a) + "\n");

Categories

Resources