ExcelPackage 읽기, excel 내보내기

5645 단어
ExcelPackage 읽기, excel 내보내기 private static string GetString(object obj) { try { return obj.ToString(); } catch (Exception ex) { return ""; } } /// /// Excel DataTable (Excel sheet) /// /// /// public DataTable WorksheetToTable(string filePath) { try { FileInfo existingFile = new FileInfo(filePath); ExcelPackage package = new ExcelPackage(existingFile); ExcelWorksheet worksheet = package.Workbook.Worksheets[1];// return WorksheetToTable(worksheet); } catch (Exception) { throw; } } /// /// worksheet datatable /// /// worksheet /// datatable public static DataTable WorksheetToTable(ExcelWorksheet worksheet) { // worksheet int rows = worksheet.Dimension.End.Row; // worksheet int cols = worksheet.Dimension.End.Column; DataTable dt = new DataTable(worksheet.Name); DataRow dr = null; for (int i = 1; i <= rows; i++) { if (i > 1) dr = dt.Rows.Add(); for (int j = 1; j <= cols; j++) { // datatable if (i == 1) dt.Columns.Add(GetString(worksheet.Cells[i, j].Value)); // datatable else dr[j - 1] = GetString(worksheet.Cells[i, j].Value); } } return dt; }
 
posted on
2018-08-02 16:40 무영비솜검독해(
...) 설명(
...) 모음 편집
다음으로 전송:https://www.cnblogs.com/itslives-com/p/ExcelPackage.html

좋은 웹페이지 즐겨찾기