일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- build
- 원한
- HMI
- SCADA
- Linux
- DataTables
- 리눅스
- JQuery
- geckodriver
- ORA-28002
- 분노
- checkbox
- 맥코트
- 파이썬
- Anaconda
- LOG
- 가상환경
- 말라키
- Eclipse
- error
- STS
- Custom
- 명령어
- Python
- pythoncom37.dll
Archives
- Today
- Total
2010년 5월 1일, 2막
[C#] OpenXML을 이용하여 Excel 파일의 빈 칸 읽기 본문
출처 : http://angeleyes.tistory.com/290
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.IO; using System.Text.RegularExpressions; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; using DocumentFormat.OpenXml.Wordprocessing; namespace WebApplication1.OpenXml { public partial class ReadExcelData : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(@"C:\test.xlsx", true)) { IEnumerablesheets = myDoc.WorkbookPart.Workbook.Descendants (); WorksheetPart worksheetPart1 = myDoc.WorkbookPart.GetPartById(sheets.First().Id) as WorksheetPart; Worksheet sheet = worksheetPart1.Worksheet; IEnumerable datarow = from row in sheet.Descendants
() where row.RowIndex > 0 select row; DataTable dt = new DataTable("test"); int i = 0; foreach (Row row in datarow) { if (i == 0) { foreach (Cell cell in row.Descendants
()) { dt.Columns.Add(new DataColumn(GetColumnName(cell.CellReference), typeof(string))); } } else { DataRow dr = dt.NewRow(); foreach (Cell cell in row.Descendants | ()) { dr[GetColumnName(cell.CellReference)] = GetCellValue(myDoc, cell, myDoc); } dt.Rows.Add(dr); } i++; } dt.WriteXml(@"C:\test\test11.xml"); } } private string GetColumnName(string cellReference) { Regex regex = new Regex("[A-Za-z]+"); Match match = regex.Match(cellReference); return match.Value; } private string GetCellValue(SpreadsheetDocument document, Cell cell, SpreadsheetDocument myDoc) { SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart; string value = cell.CellValue != null ? cell.CellValue.InnerXml : String.Empty; if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString) return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText; else if (cell.StyleIndex.InnerText.Equals("1")) { string strDate = String.Empty; DateTime cellDate = DateTime.FromOADate(Convert.ToDouble(cell.CellValue.InnerText)); if (cellDate != null) strDate = cellDate.ToString("yyyy-MM-dd"); return strDate; } else return value; } } } |
'Computer' 카테고리의 다른 글
[C#] 상속받은 Form의 버튼을 수정하고자 할 때 (0) | 2013.04.02 |
---|---|
[DevExpress] How to remove "Drag a column header here to group by that column" (0) | 2013.04.02 |
[C#] From Excel to DataTable in C# with Open XML (0) | 2013.03.29 |
[C#] DataGridView에서 원하는 값을 얻어오는 방법 (0) | 2013.03.28 |
[C#] OpenFileDialog 사용법 (0) | 2013.03.28 |