-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelExtendtion.cs
More file actions
71 lines (68 loc) · 2.39 KB
/
Copy pathExcelExtendtion.cs
File metadata and controls
71 lines (68 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Concurrent;
using System.Data;
namespace ExportExcelByMultithreading
{
public static class ExcelExtendtion
{
private static int count = 1;
private static object lockObj = new object();
public static void CreatExcel(DataTable dt)
{
IWorkbook workbook;
workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Sheet1");
IRow header = sheet.CreateRow(0);
for (int i = 0; i < dt.Columns.Count; i++)
{
ICell cell = header.CreateCell(i);
cell.SetCellValue(dt.Columns[i].ColumnName);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow row = sheet.CreateRow(i + 1);
for (int j = 0; j < dt.Columns.Count; j++)
{
ICell cell = row.CreateCell(j);
cell.SetCellValue(dt.Rows[i][j].ToString());
}
}
var fileName = $@".\{DateTime.Now.Ticks}.xlsx";
using FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
workbook.Write(fs);
}
public static void Insert(BlockingCollection<User> users, ISheet sheet)
{
while (!users.IsCompleted) {
var lockTaken = false;
while (lockTaken == false) {
Monitor.TryEnter(lockObj, 100, ref lockTaken);
}
try
{
User? user = null;
users.TryTake(out user);
Console.WriteLine(users.IsCompleted);
//Console.WriteLine(users.IsCompleted);
var row = sheet.CreateRow(count);
if (user != null)
{
row.CreateCell(0).SetCellValue(user.Id);
row.CreateCell(1).SetCellValue(user.Name);
Console.WriteLine(count.ToString());
count++;
}
}
catch (Exception e)
{
throw e;
}
finally
{
Monitor.Exit(lockObj);
}
}
}
}
}