public void TestDatabaseTypeDeserialization(string dataType, string value) { string json = string.Format(@"{{""dataType"":""{0}"",""value"":""{1}""}}", dataType, value); using (var str = new StringReader(json)) { var container = JSON.Deserialize <ObjectContainer>(str, Options); DatabaseType databaseType = DatabaseTypeHelper.ConvertFromDisplayName(dataType); string stringVal = container.Value; object val = databaseType.ConvertFromString(stringVal); if (val is DateTime) { DateTime val1 = ( DateTime )val; DateTime val2 = DateTime.Parse(value); Assert.AreEqual(val1, val2); } else { Assert.AreEqual(value.ToLowerInvariant( ), val.ToString( ).ToLowerInvariant( )); } } }
/// <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> /// Deserializes the TypedValue from xml. /// </summary> /// <param name="reader">The reader.</param> /// <param name="target">The target.</param> internal static void FromXml_Impl(XmlReader reader, TypedValue target) { // Current formats: // <TypedValue type="String">Hello</TypedValue> // <TypedValue type="Int32">1234</TypedValue> // <TypedValue type="InlineRelationship" entityRef="true">123<SourceEntityTypeId entityRef="true">456</SourceEntityTypeId></TypedValue> // etc.. // Legacy (pre 1.0) formats: // <TypedValue><Value xsi:type="xsd:int">4</Value><Type xsi:type="Int32Type" /></TypedValue> // <TypedValue> // <Type xsi:type="InlineRelationshipType"> // </Type> // <SourceEntityTypeId>5678</SourceEntityTypeId> // <Value>1234:Not Available;11293:unknown;</Value> // </TypedValue> // etc.. bool typeNameUsesDisplayName = false; string valueString = string.Empty; // Read 'type' attribute string typeName = reader.GetAttribute("type"); if (typeName != null) { typeName = typeName + "Type"; } // Read any content nodes bool rootIsEmpty = reader.IsEmptyElement; int depth = rootIsEmpty ? 0 : 1; bool isLegacyValueElem = false; bool isLegacyTypeElem = false; bool isSourceEntityTypeIdElem = false; reader.Read(); while (depth > 0) { switch (reader.NodeType) { case XmlNodeType.Element: isLegacyValueElem = false; isLegacyTypeElem = false; isSourceEntityTypeIdElem = false; if (depth == 1) { switch (reader.Name) { // Legacy <Value> element case "Value": case "value": // (conditional formatting) isLegacyValueElem = true; break; // Legacy <type> element (conditional formatting) .. type might be in text or name attribute. case "type": isLegacyTypeElem = true; if (typeName == null) { typeName = reader.GetAttribute("name"); if (typeName != null) { typeNameUsesDisplayName = true; } } break; // Legacy <Type> element case "Type": if (typeName == null) { typeName = reader.GetAttribute("type", "http://www.w3.org/2001/XMLSchema-instance"); // xsi:type } break; // Deprecated <SourceEntityTypeId> element case "SourceEntityTypeId": // deprecated isSourceEntityTypeIdElem = true; break; } } if (!reader.IsEmptyElement) { depth++; } break; case XmlNodeType.Text: if (!string.IsNullOrEmpty(reader.Value)) { // Depth == 1 : Current Value as Text // Also, legacy Value element if (depth == 1 || isLegacyValueElem) { valueString = reader.Value; } // SourceEntityTypeId element (current) else if (isSourceEntityTypeIdElem) { target.SourceEntityTypeId = XmlConvert.ToInt64(reader.Value); } // Legacy Type element else if (isLegacyTypeElem && typeName == null) { typeName = reader.Value; typeNameUsesDisplayName = true; } } break; case XmlNodeType.EndElement: depth--; break; case XmlNodeType.None: throw new Exception("Unexpected end of XML."); } reader.Read(); } // Validate type if (typeName == null) { throw new InvalidOperationException("No type data"); } // Perform value-string corrections to legacy (pre 1.0) format. if (typeName == "ChoiceRelationshipType" || typeName == "InlineRelationshipType" || typeName == "IdentifierType") { if (valueString.EndsWith(";")) { valueString = valueString.Split(':')[0]; } } if (typeNameUsesDisplayName) { // Legacy target.Type = DatabaseTypeHelper.ConvertFromDisplayName(typeName); } else { // Current target.Type = DatabaseTypeHelper.ConvertDatabaseTypeNameToDatabaseType(typeName); } if (rootIsEmpty && target.Type is StringType) { target.ValueString = null; } else { target.ValueString = valueString ?? ""; } }
/// <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; } }