-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from SyncfusionExamples/880806-ExportDataTable…
…WithBase64Images ExportDataTableWithBase64Images
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...64Images/NET Standard/ExportDataTableWithBase64Images/ExportDataTableWithBase64Images.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.9.34310.174 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExportDataTableWithBase64Images", "ExportDataTableWithBase64Images\ExportDataTableWithBase64Images.csproj", "{4F625560-14C5-4115-B04B-741D432DEDE8}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{4F625560-14C5-4115-B04B-741D432DEDE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{4F625560-14C5-4115-B04B-741D432DEDE8}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{4F625560-14C5-4115-B04B-741D432DEDE8}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{4F625560-14C5-4115-B04B-741D432DEDE8}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {4E506ED1-B2FE-4829-8F37-FC5BB41D8126} | ||
EndGlobalSection | ||
EndGlobal |
Binary file added
BIN
+8.72 KB
...d/ExportDataTableWithBase64Images/ExportDataTableWithBase64Images/Data/InputTemplate.xlsx
Binary file not shown.
15 changes: 15 additions & 0 deletions
15
...leWithBase64Images/ExportDataTableWithBase64Images/ExportDataTableWithBase64Images.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="25.1.38" /> | ||
</ItemGroup> | ||
|
||
</Project> |
96 changes: 96 additions & 0 deletions
96
...s/NET Standard/ExportDataTableWithBase64Images/ExportDataTableWithBase64Images/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System.Data; | ||
using Syncfusion.XlsIO; | ||
using Newtonsoft.Json; | ||
|
||
|
||
namespace ExportDataTableWithBase64Images | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
//Create an instance of ExcelEngine | ||
using (ExcelEngine excelEngine = new ExcelEngine()) | ||
{ | ||
string mergeData = GetData(); | ||
DataTable dtExport = (DataTable)JsonConvert.DeserializeObject(mergeData, typeof(DataTable)); | ||
|
||
//Change the image column datatype to byte[] | ||
ChangeColumnType(dtExport, "Signature", typeof(byte[])); | ||
|
||
//Instantiate the Excel application object | ||
IApplication application = excelEngine.Excel; | ||
|
||
//Create a new workbook | ||
FileStream inputStream = new FileStream("../../../Data/InputTemplate.xlsx",FileMode.Open, FileAccess.Read); | ||
IWorkbook workbook = application.Workbooks.Open(inputStream); | ||
IWorksheet worksheet = workbook.Worksheets[0]; | ||
|
||
//Create template marker processor | ||
ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor(); | ||
|
||
//Add marker variable | ||
marker.AddVariable("GeneratedData", dtExport); | ||
|
||
//Apply markers | ||
marker.ApplyMarkers(); | ||
|
||
//Autofit the columns | ||
worksheet.UsedRange.AutofitColumns(); | ||
|
||
//Save the workbook | ||
FileStream fileStream = new FileStream("Output.xlsx",FileMode.Create,FileAccess.Write); | ||
workbook.SaveAs(fileStream); | ||
|
||
System.Diagnostics.Process process = new System.Diagnostics.Process(); | ||
process.StartInfo = new System.Diagnostics.ProcessStartInfo("Output.xlsx") | ||
{ | ||
UseShellExecute = true | ||
}; | ||
process.Start(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Change the image column type to byte[] | ||
/// </summary> | ||
/// <param name="dataTable">data table</param> | ||
/// <param name="columnName">column name of the datatable</param> | ||
/// <param name="newType">new type of the column</param> | ||
public static void ChangeColumnType(DataTable dataTable, string columnName, Type newType) | ||
{ | ||
if (dataTable.Columns.Contains(columnName)) | ||
{ | ||
// Create a new column with the new data type | ||
DataColumn newColumn = new DataColumn("Images", newType); | ||
|
||
// Add the new column to the DataTable | ||
dataTable.Columns.Add(newColumn); | ||
|
||
// Copy data from the old column to the new column | ||
foreach (DataRow row in dataTable.Rows) | ||
{ | ||
// convert the base64 image to byte[] | ||
byte[] imageData = Convert.FromBase64String(row[columnName].ToString()); | ||
row[columnName] = imageData; | ||
|
||
// Set the image data for the new column | ||
row[newColumn] = imageData; | ||
} | ||
|
||
// Remove the old column | ||
dataTable.Columns.Remove(columnName); | ||
newColumn.ColumnName = columnName; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Json Data for populating a datatable | ||
/// </summary> | ||
/// <returns></returns> | ||
private static string GetData() | ||
{ | ||
return "[{\"Name\": \"John\",\"LastName\": \"Doe\",\"Address\": \"123 Main St\",\"City\": \"New York\",\"Telephone\": \"987-654-3210\",\"Email\": \"[email protected]\",\"Signature\": \"iVBORw0KGgoAAAANSUhEUgAAAQ4AAACUCAMAAABV5TcGAAAAclBMVEX////+/v4AAAD///37+/v19fXv7+/4+Pjo6Ojd3d39/f/k5OTX19fr6+u3t7fS0tKgoKDJycnCwsJ1dXWvr6+Ojo6pqalCQkIdHR01NTU7OzsrKyuXl5eIiIhsbGx7e3tPT08PDw9iYmJZWVkkJCQXFxeXGSxBAAAPq0lEQVR4nO1dh3rqOgy2TWaTOLEzWNmB93/FK8lh9LQUCO0tUP6vp+3JwlJkbbuMvfDCCy+88MILL7zwwgsvvPApuPk+/sBf+G8O5xdhcYA1OzrC8dCvjeeXMRLu+EoWSZFp38WDf5cfzI6LZpgvl5vFYrFZL9smZuyPThbuJ/VqsxUGXUc/to3zd9jBDfA33SwNHzZ1pkLPse3I1yn8fxkw/lcmjEWv3pK9YUWbxO/Pu3knumhvaP4AuK6JFfOcWGEk4UB+thbtX2EHt4IElEW3HhKfpgznb+RpEPVmIoF8xM/ODhIBy1GoHNZtGVrvTh59B7M7F/Uf0Ka2J1shFvNa2ez0bIDDjRBP7XwgaXZYgiVZ9olHB6zTF7OsE6dPPzRmzHjgjsrXoDwbaZl5c/rdwwm1Fd5TzpYZ5zPgh61ykIw2V+SDU5x2+h6O7Aielh3MJWYMZXzpDNBb8Xye6cxME67yFTAjIVtymX5MOvGTA/sdzGZIe5jPgRlFaO28irPgxrI8l3gYpREkAyjQJHyX4Tl7aw9u6XPZWY5Kw876rViUoX3dvd5KJM8lG/Ry4xRsa4PMuIY2zuQGnPSn4gdww0MN2scOqQzrGuJqsbWegx1jIAb/ZNuJlXTOOV2fPMGbi/SJIlqUhKCBsDX3riYJe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