public void TestFromFile() { IWorkbook workbook = HSSFTestDataSamples.OpenSampleWorkbook("Formatting.xls"); ISheet sheet = workbook.GetSheetAt(0); HSSFDataFormatter f = new HSSFDataFormatter(); // This one is one of the nasty auto-locale changing ones... Assert.AreEqual("dd/mm/yyyy", sheet.GetRow(1).GetCell(0).StringCellValue); Assert.AreEqual("m/d/yy", sheet.GetRow(1).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("11/24/06", f.FormatCellValue(sheet.GetRow(1).GetCell(1))); Assert.AreEqual("yyyy/mm/dd", sheet.GetRow(2).GetCell(0).StringCellValue); Assert.AreEqual("yyyy/mm/dd", sheet.GetRow(2).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("2006/11/24", f.FormatCellValue(sheet.GetRow(2).GetCell(1))); Assert.AreEqual("yyyy-mm-dd", sheet.GetRow(3).GetCell(0).StringCellValue); Assert.AreEqual("yyyy\\-mm\\-dd", sheet.GetRow(3).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("2006-11-24", f.FormatCellValue(sheet.GetRow(3).GetCell(1))); Assert.AreEqual("yy/mm/dd", sheet.GetRow(4).GetCell(0).StringCellValue); Assert.AreEqual("yy/mm/dd", sheet.GetRow(4).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("06/11/24", f.FormatCellValue(sheet.GetRow(4).GetCell(1))); // Another builtin fun one Assert.AreEqual("dd/mm/yy", sheet.GetRow(5).GetCell(0).StringCellValue); Assert.AreEqual("d/m/yy;@", sheet.GetRow(5).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("24/11/06", f.FormatCellValue(sheet.GetRow(5).GetCell(1))); Assert.AreEqual("dd-mm-yy", sheet.GetRow(6).GetCell(0).StringCellValue); Assert.AreEqual("dd\\-mm\\-yy", sheet.GetRow(6).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("24-11-06", f.FormatCellValue(sheet.GetRow(6).GetCell(1))); // Another builtin fun one Assert.AreEqual("nn.nn", sheet.GetRow(9).GetCell(0).StringCellValue); Assert.AreEqual("General", sheet.GetRow(9).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("10.52", f.FormatCellValue(sheet.GetRow(9).GetCell(1))); // text isn't quite the format rule... Assert.AreEqual("nn.nnn", sheet.GetRow(10).GetCell(0).StringCellValue); Assert.AreEqual("0.000", sheet.GetRow(10).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("10.520", f.FormatCellValue(sheet.GetRow(10).GetCell(1))); // text isn't quite the format rule... Assert.AreEqual("nn.n", sheet.GetRow(11).GetCell(0).StringCellValue); Assert.AreEqual("0.0", sheet.GetRow(11).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("10.5", f.FormatCellValue(sheet.GetRow(11).GetCell(1))); // text isn't quite the format rule... Assert.AreEqual("\u00a3nn.nn", sheet.GetRow(12).GetCell(0).StringCellValue); Assert.AreEqual("\"\u00a3\"#,##0.00", sheet.GetRow(12).GetCell(1).CellStyle.GetDataFormatString()); Assert.AreEqual("\u00a310.52", f.FormatCellValue(sheet.GetRow(12).GetCell(1))); }
public void TestGeneralAtFormat() { IWorkbook workbook = HSSFTestDataSamples.OpenSampleWorkbook("47154.xls"); ISheet sheet = workbook.GetSheetAt(0); IRow row = sheet.GetRow(0); ICell cellA1 = row.GetCell(0); Assert.AreEqual(CellType.NUMERIC, cellA1.CellType); Assert.AreEqual(2345.0, cellA1.NumericCellValue, 0.0001); Assert.AreEqual("@", cellA1.CellStyle.GetDataFormatString()); HSSFDataFormatter f = new HSSFDataFormatter(); Assert.AreEqual("2345", f.FormatCellValue(cellA1)); }
/// <summary> /// Returns a string representation of the cell /// This method returns a simple representation, /// anthing more complex should be in user code, with /// knowledge of the semantics of the sheet being Processed. /// Formula cells return the formula string, /// rather than the formula result. /// Dates are Displayed in dd-MMM-yyyy format /// Errors are Displayed as #ERR<errIdx> /// </summary> public override String ToString() { string format = this.CellStyle.GetDataFormatString(); HSSFDataFormatter formatter = new HSSFDataFormatter(); return formatter.FormatCellValue(this); }
ToxyTable Parse(IWorkbook workbook, int sheetIndex, bool extractHeader, bool extractFooter, bool hasHeader, bool fillBlankCells, bool includeComment, HSSFDataFormatter formatter) { ToxyTable table = new ToxyTable(); if (workbook.NumberOfSheets - 1 < sheetIndex) { throw new ArgumentOutOfRangeException(string.Format("This file only contains {0} sheet(s).", workbook.NumberOfSheets)); } ISheet sheet = workbook.GetSheetAt(sheetIndex); table.Name = sheet.SheetName; if (extractHeader && sheet.Header != null) { table.PageHeader = sheet.Header.Left + "|" + sheet.Header.Center + "|" + sheet.Header.Right; } if (extractFooter && sheet.Footer != null) { table.PageFooter = sheet.Footer.Left + "|" + sheet.Footer.Center + "|" + sheet.Footer.Right; } bool firstRow = true; table.LastRowIndex = sheet.LastRowNum; foreach (IRow row in sheet) { ToxyRow tr = null; if (!hasHeader || !firstRow) { tr = new ToxyRow(row.RowNum); } else if (hasHeader && firstRow) { table.HeaderRows.Add(new ToxyRow(row.RowNum)); } foreach (ICell cell in row) { if (hasHeader && firstRow) { table.HeaderRows[0].Cells.Add(new ToxyCell(cell.ColumnIndex, cell.ToString())); } else { if (tr.LastCellIndex < cell.ColumnIndex) { tr.LastCellIndex = cell.ColumnIndex; } ToxyCell c = new ToxyCell(cell.ColumnIndex, formatter.FormatCellValue(cell)); if (!string.IsNullOrEmpty(cell.ToString())) { tr.Cells.Add(c); } else if (fillBlankCells) { tr.Cells.Add(c); } if (includeComment && cell.CellComment != null) { c.Comment = cell.CellComment.String.String; } } } if (tr != null) { tr.RowIndex = row.RowNum; table.Rows.Add(tr); if (table.LastColumnIndex < tr.LastCellIndex) table.LastColumnIndex = tr.LastCellIndex; } if (firstRow) { firstRow = false; } } for (int j = 0; j < sheet.NumMergedRegions; j++) { var range = sheet.GetMergedRegion(j); table.MergeCells.Add(new MergeCellRange() { FirstRow = range.FirstRow, FirstColumn = range.FirstColumn, LastRow = range.LastRow, LastColumn = range.LastColumn }); } return table; }
public ToxySpreadsheet Parse() { if (!File.Exists(Context.Path)) throw new FileNotFoundException("File " + Context.Path + " is not found"); bool hasHeader = false; if (Context.Properties.ContainsKey("GenerateColumnHeader")) { hasHeader = Utility.IsTrue(Context.Properties["GenerateColumnHeader"]); } bool extractHeader = false; if (Context.Properties.ContainsKey("ExtractSheetHeader")) { extractHeader = Utility.IsTrue(Context.Properties["ExtractSheetHeader"]); } bool extractFooter = false; if (Context.Properties.ContainsKey("ExtractSheetFooter")) { extractFooter = Utility.IsTrue(Context.Properties["ExtractSheetFooter"]); } bool showCalculatedResult = false; if (Context.Properties.ContainsKey("ShowCalculatedResult")) { showCalculatedResult = Utility.IsTrue(Context.Properties["ShowCalculatedResult"]); } bool fillBlankCells = false; if (Context.Properties.ContainsKey("FillBlankCells")) { fillBlankCells = Utility.IsTrue(Context.Properties["FillBlankCells"]); } bool includeComment = true; if (Context.Properties.ContainsKey("IncludeComments")) { includeComment = Utility.IsTrue(Context.Properties["IncludeComments"]); } ToxySpreadsheet ss = new ToxySpreadsheet(); ss.Name = Context.Path; IWorkbook workbook = WorkbookFactory.Create(Context.Path); HSSFDataFormatter formatter = new HSSFDataFormatter(); for (int i = 0; i < workbook.NumberOfSheets; i++) { ToxyTable table=new ToxyTable(); ISheet sheet = workbook.GetSheetAt(i); table.Name = sheet.SheetName; if (extractHeader && sheet.Header != null) { table.PageHeader = sheet.Header.Left + "|" + sheet.Header.Center + "|" + sheet.Header.Right; } if (extractFooter && sheet.Footer != null) { table.PageFooter = sheet.Footer.Left + "|" + sheet.Footer.Center + "|" + sheet.Footer.Right; } bool firstRow = true; table.LastRowIndex = sheet.LastRowNum; foreach (IRow row in sheet) { ToxyRow tr=null; if (!hasHeader || !firstRow) { tr=new ToxyRow(row.RowNum); } foreach (ICell cell in row) { if (hasHeader&& firstRow) { table.ColumnHeaders.Cells.Add(new ToxyCell(cell.ColumnIndex, cell.ToString())); } else { if (tr.LastCellIndex < cell.ColumnIndex) { tr.LastCellIndex = cell.ColumnIndex; } ToxyCell c = new ToxyCell(cell.ColumnIndex, formatter.FormatCellValue(cell)); if (!string.IsNullOrEmpty(cell.ToString())) { tr.Cells.Add(c); } else if (fillBlankCells) { tr.Cells.Add(c); } if (cell.CellComment != null) { c.Comment = cell.CellComment.String.String; } } } if (tr != null) { tr.RowIndex = row.RowNum; table.Rows.Add(tr); } if (firstRow) { firstRow = false; } if(table.LastColumnIndex<tr.LastCellIndex) table.LastColumnIndex=tr.LastCellIndex; } for (int j = 0; j < sheet.NumMergedRegions; j++) { var range = sheet.GetMergedRegion(j); table.MergeCells.Add(new MergeCellRange() { FirstRow = range.FirstRow, FirstColumn = range.FirstColumn, LastRow = range.LastRow, LastColumn = range.LastColumn }); } ss.Tables.Add(table); } if (workbook is XSSFWorkbook) { var props= ((XSSFWorkbook)workbook).GetProperties(); if (props.CoreProperties != null) { if (props.CoreProperties.Title != null) { ss.Properties.Add("Title", props.CoreProperties.Title ); } else if (props.CoreProperties.Identifier != null) { ss.Properties.Add("Identifier", props.CoreProperties.Identifier ); } else if (props.CoreProperties.Keywords != null) { ss.Properties.Add("Keywords", props.CoreProperties.Keywords); } else if (props.CoreProperties.Revision != null) { ss.Properties.Add("Revision", props.CoreProperties.Revision); } else if (props.CoreProperties.Subject != null) { ss.Properties.Add("Subject", props.CoreProperties.Subject); } else if (props.CoreProperties.Modified != null) { ss.Properties.Add("Modified", props.CoreProperties.Modified); } else if (props.CoreProperties.LastPrinted != null) { ss.Properties.Add("LastPrinted", props.CoreProperties.LastPrinted); } else if (props.CoreProperties.Created != null) { ss.Properties.Add("Created", props.CoreProperties.Created); } else if (props.CoreProperties.Creator != null) { ss.Properties.Add("Creator", props.CoreProperties.Creator); } else if (props.CoreProperties.Description != null) { ss.Properties.Add("Description", props.CoreProperties.Description); } } if (props.ExtendedProperties != null && props.ExtendedProperties.props!=null) { var extProps = props.ExtendedProperties.props.GetProperties(); if (extProps.Application != null) { ss.Properties.Add("Application", extProps.Application); } if (extProps.AppVersion != null) { ss.Properties.Add("AppVersion", extProps.AppVersion); } if (extProps.Characters>0) { ss.Properties.Add("Characters", extProps.Characters); } if (extProps.CharactersWithSpaces>0) { ss.Properties.Add("CharactersWithSpaces", extProps.CharactersWithSpaces); } if (extProps.Company != null) { ss.Properties.Add("Company", extProps.Company); } if (extProps.Lines > 0) { ss.Properties.Add("Lines", extProps.Lines); } if (extProps.Manager != null) { ss.Properties.Add("Manager", extProps.Manager); } if (extProps.Notes> 0) { ss.Properties.Add("Notes", extProps.Notes); } if (extProps.Pages>0) { ss.Properties.Add("Pages", extProps.Pages); } if (extProps.Paragraphs>0) { ss.Properties.Add("Paragraphs", extProps.Paragraphs); } if (extProps.Words>0) { ss.Properties.Add("Words", extProps.Words); } if (extProps.TotalTime>0) { ss.Properties.Add("TotalTime", extProps.TotalTime); } } } else { //HSSFWorkbook var si = ((HSSFWorkbook)workbook).SummaryInformation; if (si != null) { if (si.Title != null) { ss.Properties.Add("Title", si.Title); } else if (si.LastSaveDateTime != null) { ss.Properties.Add("LastSaveDateTime", si.LastSaveDateTime); } else if (si.PageCount > 0) { ss.Properties.Add("PageCount", si.PageCount); } else if (si.OSVersion > 0) { ss.Properties.Add("OSVersion", si.OSVersion); } else if (si.Security > 0) { ss.Properties.Add("Security", si.Security); } else if (si.Keywords != null) { ss.Properties.Add("Keywords", si.Keywords); } else if (si.EditTime > 0) { ss.Properties.Add("EditTime", si.EditTime); } else if (si.Subject != null) { ss.Properties.Add("Subject", si.Subject); } else if (si.CreateDateTime != null) { ss.Properties.Add("CreateDateTime", si.CreateDateTime); } else if (si.LastPrinted != null) { ss.Properties.Add("LastPrinted", si.LastPrinted); } else if (si.CharCount != null) { ss.Properties.Add("CharCount", si.CharCount); } else if (si.Author != null) { ss.Properties.Add("Author", si.Author); } else if (si.LastAuthor != null) { ss.Properties.Add("LastAuthor", si.LastAuthor); } else if (si.ApplicationName != null) { ss.Properties.Add("ApplicationName", si.ApplicationName); } else if (si.RevNumber != null) { ss.Properties.Add("RevNumber", si.RevNumber); } else if (si.Template != null) { ss.Properties.Add("Template", si.Template); } } var dsi = ((HSSFWorkbook)workbook).DocumentSummaryInformation; if(dsi!=null) { if (dsi.ByteCount > 0) { ss.Properties.Add("ByteCount", dsi.ByteCount); } else if (dsi.Company !=null) { ss.Properties.Add("Company", dsi.Company); } else if (dsi.Format>0) { ss.Properties.Add("Format", dsi.Format); } else if (dsi.LineCount!= null) { ss.Properties.Add("LineCount", dsi.Company); } else if (dsi.LinksDirty) { ss.Properties.Add("LinksDirty", true); } else if (dsi.Manager!=null) { ss.Properties.Add("Manager", dsi.Manager); } else if (dsi.NoteCount != null) { ss.Properties.Add("NoteCount", dsi.NoteCount); } else if (dsi.Scale) { ss.Properties.Add("Scale", dsi.Scale); } else if (dsi.Company != null) { ss.Properties.Add("Company", dsi.Company); } else if (dsi.MMClipCount != null) { ss.Properties.Add("MMClipCount", dsi.MMClipCount); } else if (dsi.ParCount != null) { ss.Properties.Add("ParCount", dsi.ParCount); } } } return ss; }