Skip to content

Commit e3395e9

Browse files
993740-RandomValue
1 parent 251a803 commit e3395e9

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36623.8 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Parallel Write", "Parallel Write\Parallel Write.csproj", "{EF8B2D4A-88EF-4932-9607-F09D20F83E07}"
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+
{EF8B2D4A-88EF-4932-9607-F09D20F83E07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EF8B2D4A-88EF-4932-9607-F09D20F83E07}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EF8B2D4A-88EF-4932-9607-F09D20F83E07}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EF8B2D4A-88EF-4932-9607-F09D20F83E07}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

FAQ/Parallel Write/.NET/Parallel Write/Parallel Write/Output/.gitkeep

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Output\*">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Syncfusion.XlsIO;
2+
3+
class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
using (ExcelEngine excelEngine = new ExcelEngine())
8+
{
9+
IApplication application = excelEngine.Excel;
10+
application.DefaultVersion = ExcelVersion.Xlsx;
11+
IWorkbook workbook = application.Workbooks.Create(1);
12+
IWorksheet worksheet = workbook.Worksheets[0];
13+
14+
object m_lock = new object();
15+
int numberOfRows = 10;
16+
Parallel.For(1, numberOfRows, i =>
17+
{
18+
var rand = new Random();
19+
lock (m_lock)
20+
{
21+
worksheet.Range[i, 1].Value2 = string.Format("R{0}T{1}", i, rand.Next(10));
22+
worksheet.Range[i, 2].Value2 = string.Format("R{0}T{1}", i, rand.Next(10));
23+
worksheet.Range[i, 3].Value2 = DateTime.Now.AddDays(rand.NextDouble() * 10.0);
24+
worksheet.Range[i, 4].Value2 = DateTime.Now.AddDays(rand.NextDouble() * 10.0);
25+
worksheet.Range[i, 5].Value2 = rand.Next(2000);
26+
worksheet.Range[i, 6].Value2 = rand.Next(4000);
27+
worksheet.Range[i, 7].Value2 = rand.NextDouble() * 10000.0;
28+
worksheet.Range[i, 8].Value2 = rand.NextDouble() * 10000.0;
29+
}
30+
});
31+
32+
#region Save
33+
//Saving the workbook
34+
workbook.SaveAs("../../../Output/Output.xlsx");
35+
#endregion
36+
}
37+
}
38+
39+
40+
}
41+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# How to assign data to multiple rows and columns in parallel without exceptions in C#?
2+
3+
Step 1: Create a New C# Console Application Project.
4+
5+
Step 2: Name the Project.
6+
7+
Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).
8+
9+
Step 4: Include the following namespaces in the **Program.cs** file.
10+
11+
```csharp
12+
using Syncfusion.XlsIO;
13+
```
14+
15+
Step 5: Include the below code snippet in **Program.cs** to assign data to multiple rows and columns in parallel in C#.
16+
```csharp
17+
using (ExcelEngine excelEngine = new ExcelEngine())
18+
{
19+
IApplication application = excelEngine.Excel;
20+
application.DefaultVersion = ExcelVersion.Xlsx;
21+
IWorkbook workbook = application.Workbooks.Create(1);
22+
IWorksheet worksheet = workbook.Worksheets[0];
23+
24+
object m_lock = new object();
25+
int numberOfRows = 10;
26+
Parallel.For(1, numberOfRows, i =>
27+
{
28+
var rand = new Random();
29+
lock (m_lock)
30+
{
31+
worksheet.Range[i, 1].Value2 = string.Format("R{0}T{1}", i, rand.Next(10));
32+
worksheet.Range[i, 2].Value2 = string.Format("R{0}T{1}", i, rand.Next(10));
33+
worksheet.Range[i, 3].Value2 = DateTime.Now.AddDays(rand.NextDouble() * 10.0);
34+
worksheet.Range[i, 4].Value2 = DateTime.Now.AddDays(rand.NextDouble() * 10.0);
35+
worksheet.Range[i, 5].Value2 = rand.Next(2000);
36+
worksheet.Range[i, 6].Value2 = rand.Next(4000);
37+
worksheet.Range[i, 7].Value2 = rand.NextDouble() * 10000.0;
38+
worksheet.Range[i, 8].Value2 = rand.NextDouble() * 10000.0;
39+
}
40+
});
41+
42+
#region Save
43+
//Saving the workbook
44+
workbook.SaveAs("../../../Output/Output.xlsx");
45+
#endregion
46+
}
47+
```

0 commit comments

Comments
 (0)