Skip to content

Commit 43d26f4

Browse files
authored
Merge pull request #205 from SyncfusionExamples/942829-ImportDataToExcelTable
942829-How to import data to table in Excel document
2 parents a89d7a8 + 5e943eb commit 43d26f4

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36127.28 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Import data to table in Excel", "Import data to table in Excel\Import data to table in Excel.csproj", "{AD4CC4A6-A12A-43B5-94AF-2ACB987DE0A0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{AD4CC4A6-A12A-43B5-94AF-2ACB987DE0A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{AD4CC4A6-A12A-43B5-94AF-2ACB987DE0A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{AD4CC4A6-A12A-43B5-94AF-2ACB987DE0A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{AD4CC4A6-A12A-43B5-94AF-2ACB987DE0A0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {B7A699BA-582E-4EBB-95FC-4DB17EDABEE9}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Import_data_to_table_in_Excel</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\*">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>

Use Cases/Import data to table in Excel/.NET/Import data to table in Excel/Import data to table in Excel/Output/.gitkeep

Whitespace-only changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Syncfusion.XlsIO;
2+
3+
class Program
4+
{
5+
public static void Main(string[] args)
6+
{
7+
using (ExcelEngine excelEngine = new ExcelEngine())
8+
{
9+
IApplication application = excelEngine.Excel;
10+
application.DefaultVersion = ExcelVersion.Xlsx;
11+
12+
IWorkbook workbook = application.Workbooks.Create(1);
13+
IWorksheet worksheet = workbook.Worksheets[0];
14+
15+
//Sample data
16+
object[,] data = new object[,]
17+
{
18+
{ "ID", "Name", "Category", "Price" },
19+
{ 1, "Apple", "Fruit", 0.99 },
20+
{ 2, "Carrot", "Vegetable", 0.49 },
21+
{ 3, "Milk", "Dairy", 1.49 }
22+
};
23+
24+
//Import data to worksheet
25+
worksheet.ImportArray(data, 1, 1);
26+
27+
//Calculate range from data size
28+
int rowCount = data.GetLength(0);
29+
int colCount = data.GetLength(1);
30+
31+
IRange dataRange = worksheet.Range[1, 1, rowCount, colCount];
32+
33+
//Create a table (ListObject)
34+
IListObject table = worksheet.ListObjects.Create("SalesTable", dataRange);
35+
36+
//Apply built-in table style
37+
table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium9;
38+
39+
//Auto-fit columns
40+
worksheet.UsedRange.AutofitColumns();
41+
42+
#region Save
43+
//Saving the workbook
44+
FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
45+
workbook.SaveAs(outputStream);
46+
#endregion
47+
48+
//Dispose streams
49+
outputStream.Dispose();
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)