|
| 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 | +} |
0 commit comments