/// <summary> /// Get the formatted cell value for the different data types. /// </summary> private static string GetFormattedCellValue(object cellValue, DatabaseType type, bool isRelationshipType = false) { if (cellValue == null || cellValue is DBNull) { return(""); } if (type is BoolType) { string result = ExportDataHelper.GetBooleanCellValue(( bool )cellValue); return(result); } if (type is ChoiceRelationshipType || type is InlineRelationshipType || isRelationshipType) { string result = DatabaseTypeHelper.GetEntityXmlName(( string )cellValue); return(result); } else { // TODO: UTC ??!! string formatString = DatabaseTypeHelper.GetDisplayFormatString(type); string result = string.Format(formatString, cellValue); return(result); } }
/// <summary> /// Add content row to the table. /// </summary> private static TableRow AddContentRow(ReportResult reportResult, DataRow contentRow) { TableCell tc; TableRow tr = new TableRow(); int dataColIndx = 0; foreach (ReportColumn col in reportResult.Metadata.ReportColumns.Values) { if (!col.IsHidden && col.Type != "Image") { string cellValue = ExportDataHelper.GetCellValue(contentRow, dataColIndx); DatabaseType cellType = DatabaseTypeHelper.ConvertFromDisplayName(col.Type); if (!string.IsNullOrEmpty(col.AutoNumberDisplayPattern)) { cellType = DatabaseType.AutoIncrementType; } if (!string.IsNullOrEmpty(cellValue)) { cellValue = ExportDataHelper.GetFormattedCellValue(cellType, cellValue, col); if (cellType is DateTimeType || cellType is TimeType || cellType is DateType || cellType is Int32Type || cellType is NumericType <decimal> || cellType is NumericType <Single> ) { AddContentRow_NonString(tr, cellValue); } else if (cellType is StructureLevelsType) { AddContentRow_StructureLevel(tr, cellValue); } else { AddContentRow_String(tr, cellValue); } } else { tc = new TableCell(new Paragraph(new Run( new DocumentFormat.OpenXml.Wordprocessing.Text("")))); tr.Append(tc); } } dataColIndx++; } return(tr); }
/// <summary> /// Create content row of the datatable. /// </summary> private static Row CreateContentRow(ReportResult reportResults, DataRow contentRow, int rowIndex) { Row row = new Row { RowIndex = (UInt32)rowIndex }; int columnIndex = 0; int dataColumnIndex = 0; foreach (ReportColumn col in reportResults.Metadata.ReportColumns.Values) { if (!col.IsHidden && col.Type != "Image") { Cell cell; string cellValue = ExportDataHelper.GetCellValue(contentRow, dataColumnIndex); DatabaseType cellType = DatabaseTypeHelper.ConvertFromDisplayName(col.Type); if (!string.IsNullOrEmpty(col.AutoNumberDisplayPattern)) { cellType = DatabaseType.AutoIncrementType; col.Type = "AutoIncrement"; } object value = DatabaseTypeHelper.ConvertFromString(cellType, cellValue); if (string.IsNullOrEmpty(cellValue)) { cell = CreateTextCell(null); sharedStrings.TryAdd(sharedStringIndex, null); } else { DateTime dateValue; switch (col.Type) { case "DateTime": dateValue = Convert.ToDateTime(value); cell = CreateDateValueCell(dateValue, 4); break; case "Date": dateValue = Convert.ToDateTime(value); cell = CreateDateValueCell(dateValue, 1); break; case "Time": dateValue = OADateOrigin + (Convert.ToDateTime(value)).ToUniversalTime().TimeOfDay; cell = CreateDateValueCell(dateValue, 10); break; case "Int32": cell = CreateNumericCell(value, 5); break; case "Decimal": cell = CreateNumericCell(value, 6); break; case "Currency": cell = CreateNumericCell(value, 9); break; case "AutoIncrement": cellValue = ExportDataHelper.GetFormattedCellValue(cellType, cellValue, col); cell = CreateTextCell(null); sharedStrings.TryAdd(sharedStringIndex, cellValue); break; case "StructureLevels": cellValue = ExportDataHelper.GetStructureLevelCellValue(cellValue, true); cell = CreateTextCell(8); sharedStrings.TryAdd(sharedStringIndex, cellValue); break; default: cellValue = ExportDataHelper.GetFormattedCellValue(cellType, cellValue, col); cell = CreateTextCell(8); sharedStrings.TryAdd(sharedStringIndex, cellValue); break; } } // Append the cell SetCellLocation(cell, columnIndex, rowIndex); row.AppendChild(cell); columnIndex++; } dataColumnIndex++; } return(row); }
/// <summary> /// Add content row to the table. /// </summary> private static TableRow AddContentRow(QueryResult queryResult, DataRow contentRow) { TableCell tc; TableRow tr = new TableRow( ); int dataColIndx = 0; foreach (ResultColumn col in queryResult.Columns) { if (!col.IsHidden) { object cellValue = contentRow[dataColIndx]; if (cellValue != null) { if (col.ColumnType is AutoIncrementType) { string displayPattern = null; if (contentRow.Table.Columns[dataColIndx].ExtendedProperties.ContainsKey("DisplayPattern")) { displayPattern = ( string )contentRow.Table.Columns[dataColIndx].ExtendedProperties["DisplayPattern"]; } var intStringVal = cellValue as string; int temp; if (intStringVal != null) { temp = int.Parse(intStringVal); } else { temp = ( int )cellValue; } if (string.IsNullOrEmpty(displayPattern)) { AddContentRow_NonString(tr, col, temp); } else { AddContentRow_String(tr, col, temp.ToString(displayPattern)); } } else if (col.ColumnType is DateTimeType || col.ColumnType is TimeType || col.ColumnType is DateType || col.ColumnType is Int32Type || col.ColumnType is NumericType <decimal> || col.ColumnType is NumericType <Single> ) { AddContentRow_NonString(tr, col, cellValue); } else if (col.ColumnType is StructureLevelsType) { //Get the structure view cell value string cellText = ExportDataHelper.GetStructureLevelCellValue(( string )contentRow[dataColIndx], true); string[] views = cellText.Split(new[] { Environment.NewLine }, StringSplitOptions.None); int i = 1; tc = new TableCell( ); Paragraph paragraph = new Paragraph( ); Run run = new Run( ); foreach (string structureView in views) { run.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(structureView)); if (i != views.Length) { //Insert a break for new line. run.Append(new Break( )); } else { paragraph.Append(run); tc.Append(paragraph); tr.Append(tc); } i++; } } else { AddContentRow_String(tr, col, cellValue); } } else { tc = new TableCell(new Paragraph(new Run( new DocumentFormat.OpenXml.Wordprocessing.Text("")))); tr.Append(tc); } } dataColIndx++; } return(tr); }
/// <summary> /// Generate CSV document byte stream /// </summary> /// <param name="reportResult">ReportResult</param> /// <param name="rows">The rows.</param> /// <returns> /// byte[] /// </returns> public static byte[] CreateCsvDocument(ReportResult reportResult, List <DataRow> rows) { var currentCulure = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; try { var sb = new StringBuilder(); int headerIndx = 1; foreach (ReportColumn col in reportResult.Metadata.ReportColumns.Values) { //add separator if (!col.IsHidden && col.Type != "Image") { sb.Append(col.Title); if (headerIndx != reportResult.Metadata.ReportColumns.Count) { sb.Append(','); } } headerIndx++; } //append new line sb.Append("\r\n"); foreach (DataRow row in rows) { int colIndx = 0; bool first = true; foreach (ReportColumn col in reportResult.Metadata.ReportColumns.Values) { if (!col.IsHidden && col.Type != "Image") { if (first) { first = false; } else { sb.Append(','); } string cellValue = ExportDataHelper.GetCellValue(row, colIndx); DatabaseType cellType = DatabaseTypeHelper.ConvertFromDisplayName(col.Type); if (!string.IsNullOrEmpty(col.AutoNumberDisplayPattern)) { cellType = DatabaseType.AutoIncrementType; } string csvValue = !string.IsNullOrEmpty(cellValue) ? ExportDataHelper.GetCsvCellValue(cellType, cellValue, col) : ""; csvValue = csvValue.Replace("\r", string.Empty).Replace("\n", string.Empty); sb.Append(CsvEscape(csvValue)); } colIndx++; } //append new line sb.Append("\r\n"); } byte[] bytesInStream = Encoding.UTF8.GetBytes(sb.ToString()); //return bytesInStream; return(Encoding.UTF8.GetPreamble().Concat(bytesInStream).ToArray()); } finally { Thread.CurrentThread.CurrentCulture = currentCulure; } }