/// <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> /// 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; } }