Repository navigation
Expand file tree
/
Copy pathExcelWriter.cs
More file actions
78 lines (64 loc) · 1.8 KB
/
Copy pathExcelWriter.cs
File metadata and controls
78 lines (64 loc) · 1.8 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
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using OfficeOpenXml;
namespace Hansoft.Excel
{
public class ExcelWriter
{
public class Cell
{
private string value;
public Cell(string value)
{
this.value = value;
}
public string Value
{
get { return value; }
}
}
public class Row
{
private List<Cell> cells;
public Row()
{
cells = new List<Cell>();
}
public List<Cell> Cells
{
get { return cells; }
}
public Cell AddCell(string value)
{
Cell cell = new Cell(value);
cells.Add(cell);
return cell;
}
}
private List<Row> rows;
public ExcelWriter()
{
rows = new List<Row>();
}
public Row AddRow()
{
Row row = new Row();
rows.Add(row);
return row;
}
public void SaveAsOfficeOpenXml(string fileName)
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);
if (fileInfo.Exists)
fileInfo.Delete();
ExcelPackage excelPackage = new ExcelPackage(fileInfo);
ExcelWorksheet sheet1 = excelPackage.Workbook.Worksheets.Add("Sheet1");
for (int rowInd= 0; rowInd <rows.Count; rowInd++)
for (int colInd= 0; colInd<rows[rowInd].Cells.Count; colInd++)
sheet1.Cells[rowInd+1, colInd+1].Value = rows[rowInd].Cells[colInd].Value;
excelPackage.Save();
}
}
}