I was going through the following tutorial to write dates using poi API in an excel sheet
http://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells
but when I tried to run the code the date filed shows "###" and not the actual date!!
Here is the code snippet
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
Cell cell= row.createCell(4);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
Excel displays a cell full of "#" signs (e.g. "########") when a cell is formatted in such a way that the resulting text is too long to display all of it in the cell. When you open the spreadsheet in Excel, widen the column long enough and it will display the formatted date.
To get the spreadsheet to be created by POI in such a way that the column will be wide enough from the start, then you need to set the column width yourself.
sheet.setColumnWidth(columnIndex, width)
Multiply your width by 256 before passing it in, because the width units used by this method are 1/256ths of a character. Here's the Javadoc: Sheet
Related
I am trying to change the cell type from general to numeric of an excel sheet using Apache Poi. I have made the .xlsx file on my own from a .csv file using Poi itself. Now I need to change the cell_type of "E" and "F" column of the xlsx file to numeric. This is what I've tried:
FileInputStream fis = new FileInputStream(final_report_name);
XSSFWorkbook workBook = new XSSFWorkbook(fis);
XSSFSheet new_sheet = workBook.getSheetAt(0);
XSSFDataFormat format = workBook.createDataFormat();
XSSFCellStyle style = workBook.createCellStyle();
style.setDataFormat(format.getFormat("#"));
new_sheet.setDefaultColumnStyle(4, style); //These don't work
new_sheet.setDefaultColumnStyle(5, style); //They keep the cell type general
XSSFRow row = new_sheet.getRow(3);
Cell cell = row.getCell(5); //This changed the cell type to "#"
cell.setCellStyle(style); // But the number is still displayed as text
FileOutputStream fileOutputStream = new FileOutputStream(final_report_name);
workBook.write(fileOutputStream);
workBook.close();
The column styling does not work at all, and the cell styling worked, but the number is still displayed as text. I still need to manually go to the hovering icon next to it which says "The number in this cell is formatted as text or preceded by an apostrophe". then I need to click on "Convert to number" menu (see image). What could be the problem here and how do I achieve this?
Setting the cell style does not automatically change the cell type. The cell type depends on the cell content. Not even explicitly setting the cell type using Cell.setCellType will always automatically change the cell type if content don't match that type. So you need setting the cell content aka cell value correct.
There are various setCellValue methods in Cell. If setCellValue(java.lang.String value) or setCellValue(RichTextString value) is used, the cell type will always be text. Do using setCellValue(double value) for numeric values, setCellValue(boolean value) for boolean values (TRUE or FALSE) and one of the various setCellValue(DATETYPE) (setCellValue(java.util.Calendar value), setCellValue(java.time.LocalDateTime value), ...) for date values.
I'm using OfficeOpenXml to create an MS/Excel spreadsheet file. Some of the columns contain arbitrary text values. However, when cells in those columns are filled with numeric values (i.e., text values containing only digits), Excel displays those cells with a small green triangle in the corner, along with the warning that "The number in this cell is formatted as text or preceded by an apostrophe".
Which is technically correct, but I do not want Excel to display the warning.
So how do I format those columns, or the cells in those columns, to be strictly text values, so that they will not be flagged as numeric text values? How do I disable the warning, and force Excel to accept those cell values as text (only)?
Note: I've seen solutions for other OpenXML packages, but none specifically for OfficeOpenXml. I've also seen solutions for interpreting text cell values as numbers, but this is the exact opposite of what I want to do.
Using DocumentFormat.OpenXml code in C# will look like this.
// Append sheetData to worksheet
worksheet.Append(sheetData);
// Ignoring errors for parsing
IgnoredErrors errors = new IgnoredErrors();
errors.AddChild(new IgnoredError() { NumberStoredAsText = true, SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A:Z" } });
// of type Worksheet
worksheet.Append(errors);
I was looking for the same answer and I think this will get you close.
After the SheetData section, you need to create an IgnoredErrors section with IgnoredError elements. You can do a range such as:
<ignoredErrors>
<ignoredError numberStoredAsText="1" sqref="G2:G10"/>
</ignoredErrors>
You can create more than one element and you can span other types of fields.
In my case, I used the range "A1:{LastCell}" and it worked.
I figured it out by creating my "bad" xlsx document, going into excel and marking the range to ignore and saving as a copy. Then I used the Open XML 2.5 productivity tool compare files to see the difference. It didn't take long to find ignorederrors section and go from there.
Another solution is to us LoadFromText, which fills the cell text and seems to suppress the 'numeric text' warnings for the cell. So I use code like this for filling the cells that have this problem:
DataRow dr = ...; // Query result row
...
cell[r, c].LoadFromText(Convert.ToString(dr["item"]));
I am creating an excel object from a vb.net listview table simply by creating an array of F(x,y), creating a range and putting the values from the array in the range as follows.
shXL.Range(Startcell,AEndCell).Value = F
However some of the fields are numeric and I want them to be formatted to two decimal places and EXCEL to recognize them as decimals
What I end up with in the excel worksheet is many green triangles telling me they are text fields.
How do I convert a range withing the sheet say A5,I20 to be formated as decimals.
I tried: (x,y).numberformat = "00.00" which works to format to 2dp but still treats the cells as text.
Furthermore, is it possible to Excel Sum a range? How is the possible?
Your help is appreciated!
shXL.Range(Startcell,AEndCell).Value = F
'// Loop over same range and convert to decimal
For Each cell In shXl.Range(Startcell,AEndCell)
With cell
.Value = CDec(.Value)
End With
Next
Furthermore, is it possible to Excel Sum a range? How is the possible?
I don't think Excel would have got very far as a spreadsheet product if it couldn't sum a range!
Assuming you want to do this in vb.net you need to use the instance of the application. I'll assume in your code it's XL
mySumValue = XL.WorksheetFunction.Sum(shXL.Range(Startcell,AEndCell))
I have a column in Excel Sheet which contains all the dates in custom dd-mm-yyyy format. I need to convert all these dates to text without losing the format. If I am changing the cell format to Text, I am losing the format. I tried to copy the cell values as values but did not work. I searched a lot on the internet, but did not find any useful resource. What's the possible solution?
Try using the TEXT function.
=TEXT(A1,"dd-mm-yyyy")
Use this formula for keeping the long date format from "A1" cell in another cell (exp: "B1"):
=TEXT(A1,"[$-F800]dddd, mmmm dd, yyyy")
The cell "A1" is a cell contains a date (such as today date with «today()» formula) with long date format.
Also, you can use this VBA code for getting same format with specify font and size in print's header:
ActiveSheet.PageSetup.RightHeader = "&""Arial Rounded MT Bold,Regular""&16" & Range("B1").Value
This code will shows the "B1" cell (as a text from "A1" cell) in Right of Header in Print.
I have this cell in Excel 2013. The contents of the cell is;
=xlqChangePercent($D2,$AP$1)
This function returns 1.00 which is displayed in the cell. I want this cell to display 1.00%. I tried =xlqChangePercent($D2,$AP$1)% but the cell becomes 0.01. How do I get the cell to display 1.00%?
Either append /100 to your formula and format % (retains content as a number) or format the cell with a custom format of:
#.00"%"
which results in a text string.
Format the cell to percentage - either use the menu or right-click on the cell and choose format.