일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 가상환경
- ORA-28002
- Anaconda
- 파이썬
- SCADA
- 분노
- checkbox
- 리눅스
- pythoncom37.dll
- Custom
- error
- 말라키
- 맥코트
- Linux
- JQuery
- 원한
- geckodriver
- Python
- LOG
- 명령어
- DataTables
- Eclipse
- STS
- HMI
Archives
- Today
- Total
2010년 5월 1일, 2막
[C#] From Excel to DataTable in C# with Open XML 본문
출처 : http://stackoverflow.com/questions/3321082/from-excel-to-datatable-in-c-sharp-with-open-xml
start
static void Main(string[] args)
{
DataTable dt = new DataTable();
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(@"..\..\example.xlsx", false))
{
WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
IEnumerable sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild().Elements();
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId);
Worksheet workSheet = worksheetPart.Worksheet;
SheetData sheetData = workSheet.GetFirstChild();
IEnumerable rows = sheetData.Descendants();
foreach (Cell cell in rows.ElementAt(0))
{
dt.Columns.Add(GetCellValue(spreadSheetDocument, cell));
}
foreach (Row row in rows) //this will also include your header row...
{
DataRow tempRow = dt.NewRow();
for (int i = 0; i < row.Descendants().Count(); i++)
{
tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants().ElementAt(i-1));
}
dt.Rows.Add(tempRow);
}
}
dt.Rows.RemoveAt(0); //...so i'm taking it out here.
}
public static string GetCellValue(SpreadsheetDocument document, Cell cell)
{
SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart;
string value = cell.CellValue.InnerXml;
if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
{
return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
}
else
{
return value;
}
}
| |
end
'Computer' 카테고리의 다른 글
[DevExpress] How to remove "Drag a column header here to group by that column" (0) | 2013.04.02 |
---|---|
[C#] OpenXML을 이용하여 Excel 파일의 빈 칸 읽기 (0) | 2013.03.29 |
[C#] DataGridView에서 원하는 값을 얻어오는 방법 (0) | 2013.03.28 |
[C#] OpenFileDialog 사용법 (0) | 2013.03.28 |
0. Setting (0) | 2013.03.13 |