-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExcelHelper.cs
107 lines (96 loc) · 3.7 KB
/
ExcelHelper.cs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace Rename_File_Or_Foder
{
class ExcelHelper
{
public static void GenerateExcel(DataTable dataTable, string path)
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add(dataTable);
Excel.Application excelApp = new Excel.Application();
Excel.Workbook excelWorkBook = excelApp.Workbooks.Add();
Excel._Worksheet xlWorksheet = excelWorkBook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
foreach (DataTable table in dataSet.Tables)
{
//Add a new worksheet to workbook with the Datatable name
Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
excelWorkSheet.Name = table.TableName;
// add all the columns
for (int i = 1; i < table.Columns.Count + 1; i++)
{
excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
}
// add all the rows
for (int j = 0; j < table.Rows.Count; j++)
{
for (int k = 0; k < table.Columns.Count; k++)
{
excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
}
}
}
excelWorkBook.SaveAs(path);
excelWorkBook.Close();
excelApp.Quit();
}
public static List<ListLoad> RedingExcel(string path)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
string str;
int rw = 0;
int cl = 0;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;
string[] header = new string[range.Columns.Count];
for (int cot = 1; cot <= range.Columns.Count; cot++)
{
str = (string)(range.Cells[1, cot] as Excel.Range).Value2;
header[cot-1] = str;
}
//ListLoad listLoad = new ListLoad();
//listLoad.SetValueByName("Path", "dsvcsvdgcsd");
List<ListLoad> list = new List<ListLoad>();
for (int rCnt = 2; rCnt <= rw; rCnt++)
{
ListLoad listLoad = new ListLoad();
for (int cCnt = 1; cCnt <= cl; cCnt++)
{
try
{
str = Convert.ToString((range.Cells[rCnt, cCnt] as Excel.Range).Value2);
}
catch
{
str = null;
}
Console.WriteLine(str);
listLoad.SetValueByName(header[cCnt - 1], str);
}
list.Add(listLoad);
}
xlWorkBook.Close(true, null, null);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
return list;
}
}
}