Skip to content

Commit 9b78741

Browse files
Тимошенко Антон ИвановичТимошенко Антон Иванович
authored andcommitted
Добавьте файлы проекта.
1 parent 5254306 commit 9b78741

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

ReadAndCreateExcelFile.sln

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 16
4+
VisualStudioVersion = 16.0.30503.244
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReadAndCreateExcelFile", "ReadAndCreateExcelFile\ReadAndCreateExcelFile.csproj", "{6BC3DAED-97BE-4619-8186-ACED72DD7C44}"
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+
{6BC3DAED-97BE-4619-8186-ACED72DD7C44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{6BC3DAED-97BE-4619-8186-ACED72DD7C44}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{6BC3DAED-97BE-4619-8186-ACED72DD7C44}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{6BC3DAED-97BE-4619-8186-ACED72DD7C44}.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 = {017F5965-4BF6-40E1-9375-7EC935C98B50}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using DocumentFormat.OpenXml;
2+
using DocumentFormat.OpenXml.Packaging;
3+
using DocumentFormat.OpenXml.Spreadsheet;
4+
using Newtonsoft.Json;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Data;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace ReadAndCreateExcelFile {
12+
class UserDetails {
13+
internal int Id { get; set; }
14+
15+
internal string Name { get; set; }
16+
17+
internal string City { get; set; }
18+
19+
internal string Country { get; set; }
20+
}
21+
22+
class ReadAndCreateExcelFile {
23+
static void Main() {
24+
//WriteExcelFile();
25+
ReadExcelFile();
26+
27+
Console.ReadKey();
28+
}
29+
30+
/// <summary>
31+
/// Метод создает Excel-файл.
32+
/// </summary>
33+
static void WriteExcelFile() {
34+
try {
35+
List<UserDetails> persons = new List<UserDetails>() {
36+
new UserDetails() { Id = 1001, Name = "ABCD", City = "City1", Country = "USA" },
37+
new UserDetails() { Id = 1002, Name = "PQRS", City = "City2", Country = "INDIA" },
38+
new UserDetails() { Id = 1003, Name = "XYZZ", City = "City3", Country = "CHINA" },
39+
new UserDetails() { Id = 1004, Name = "LMNO", City = "City4", Country = "UK" },
40+
};
41+
42+
DataTable table = (DataTable)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(persons), typeof(DataTable));
43+
44+
using (SpreadsheetDocument document = SpreadsheetDocument.Create(@"c:\test_excel\TestFile.xlsx", SpreadsheetDocumentType.Workbook)) {
45+
WorkbookPart workbookPart = document.AddWorkbookPart();
46+
workbookPart.Workbook = new Workbook();
47+
48+
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
49+
var sheetData = new SheetData();
50+
worksheetPart.Worksheet = new Worksheet(sheetData);
51+
52+
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
53+
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };
54+
55+
sheets.Append(sheet);
56+
57+
Row headerRow = new Row();
58+
59+
List<string> columns = new List<string>();
60+
foreach (DataColumn column in table.Columns) {
61+
columns.Add(column.ColumnName);
62+
63+
Cell cell = new Cell();
64+
cell.DataType = CellValues.String;
65+
cell.CellValue = new CellValue(column.ColumnName);
66+
headerRow.AppendChild(cell);
67+
}
68+
69+
sheetData.AppendChild(headerRow);
70+
71+
foreach (DataRow dsrow in table.Rows) {
72+
Row newRow = new Row();
73+
foreach (string col in columns) {
74+
Cell cell = new Cell();
75+
cell.DataType = CellValues.String;
76+
cell.CellValue = new CellValue(dsrow[col].ToString());
77+
newRow.AppendChild(cell);
78+
}
79+
80+
sheetData.AppendChild(newRow);
81+
}
82+
83+
workbookPart.Workbook.Save();
84+
}
85+
}
86+
catch (Exception ex) {
87+
throw new Exception(ex.Message.ToString());
88+
}
89+
}
90+
91+
/// <summary>
92+
/// Метод читает Excel-файл.
93+
/// </summary>
94+
static void ReadExcelFile() {
95+
try {
96+
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(@"c:\test_excel\TestFile.xlsx", false)) {
97+
WorkbookPart workbookPart = doc.WorkbookPart;
98+
Sheets thesheetcollection = workbookPart.Workbook.GetFirstChild<Sheets>();
99+
StringBuilder excelResult = new StringBuilder();
100+
101+
foreach (Sheet thesheet in thesheetcollection) {
102+
excelResult.AppendLine("Excel Sheet Name : " + thesheet.Name);
103+
excelResult.AppendLine("----------------------------------------------- ");
104+
Worksheet theWorksheet = ((WorksheetPart)workbookPart.GetPartById(thesheet.Id)).Worksheet;
105+
106+
SheetData thesheetdata = theWorksheet.GetFirstChild<SheetData>();
107+
foreach (Row thecurrentrow in thesheetdata) {
108+
foreach (Cell thecurrentcell in thecurrentrow) {
109+
string currentcellvalue = string.Empty;
110+
if (thecurrentcell.DataType != null) {
111+
if (thecurrentcell.DataType == CellValues.SharedString) {
112+
int id;
113+
if (int.TryParse(thecurrentcell.InnerText, out id)) {
114+
SharedStringItem item = workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(id);
115+
if (item.Text != null) {
116+
excelResult.Append(item.Text.Text + " ");
117+
}
118+
else if (item.InnerText != null) {
119+
currentcellvalue = item.InnerText;
120+
}
121+
else if (item.InnerXml != null) {
122+
currentcellvalue = item.InnerXml;
123+
}
124+
}
125+
}
126+
}
127+
else {
128+
excelResult.Append(Convert.ToInt16(thecurrentcell.InnerText) + " ");
129+
}
130+
}
131+
excelResult.AppendLine();
132+
}
133+
excelResult.Append("");
134+
Console.WriteLine(excelResult.ToString());
135+
}
136+
}
137+
}
138+
catch (Exception ex) {
139+
throw new Exception(ex.Message.ToString());
140+
}
141+
}
142+
}
143+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="DocumentFormat.OpenXml" Version="2.11.3" />
10+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)