protected _Worksheet GetExcelSheet(string path) { ExcelApp = StartExcel(); ExcelWorkbooks = ExcelApp.Workbooks; ExcelWorkbook = ExcelWorkbooks.Open(path); return(ExcelWorkbook.Sheets[1]); }
public List <ClienteBean> ReadFromExcel(string keyName) { string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ExcelPath); List <ClienteBean> clientes = new List <ClienteBean>(); Excel.Application ExcelApp = new Excel.Application(); Excel.Workbooks ExcelWorkbooks; Excel.Workbook ExcelWorkbook; Excel.Worksheet ExcelWorksheet; Excel.Range ExcelRange; ExcelWorkbooks = ExcelApp.Workbooks; ExcelWorkbook = ExcelWorkbooks.Open(path); ExcelWorksheet = ExcelWorkbook.Sheets[1]; ExcelRange = ExcelWorksheet.UsedRange; int rowCount = ExcelRange.Rows.Count; int colCount = ExcelRange.Columns.Count; string[] headers = new string[colCount]; for (int i = 1; i <= colCount; i++) { headers[i - 1] = (Convert.ToString((ExcelRange.Cells[1, i] as Excel.Range).Value2)); } for (int j = 2; j <= rowCount; j++) { string Key = Convert.ToString((ExcelRange.Cells[j, 1] as Excel.Range).Value2); if (Key.Equals(keyName)) { ClienteBean clienteBean = new ClienteBean(); clienteBean.Apellido = Convert.ToString((ExcelRange.Cells[j, 2] as Excel.Range).Value2); clienteBean.Nombre = Convert.ToString((ExcelRange.Cells[j, 3] as Excel.Range).Value2); clienteBean.Dni = Convert.ToString((ExcelRange.Cells[j, 4] as Excel.Range).Value2); clienteBean.Sexo = Convert.ToString((ExcelRange.Cells[j, 5] as Excel.Range).Value2); clienteBean.Nivelestudios = Convert.ToString((ExcelRange.Cells[j, 6] as Excel.Range).Value2); clienteBean.Telefono = Convert.ToString((ExcelRange.Cells[j, 7] as Excel.Range).Value2); clienteBean.Edad = Convert.ToString((ExcelRange.Cells[j, 8] as Excel.Range).Value2); clienteBean.Resultado = Convert.ToString((ExcelRange.Cells[j, 9] as Excel.Range).Value2); clientes.Add(clienteBean); } continue; } ExcelWorkbook.Close(); ExcelWorkbooks.Close(); ExcelApp.Quit(); Marshal.ReleaseComObject(ExcelRange); Marshal.ReleaseComObject(ExcelWorksheet); Marshal.ReleaseComObject(ExcelWorkbooks); Marshal.ReleaseComObject(ExcelWorkbook); Marshal.ReleaseComObject(ExcelApp); return(clientes); }
private void StartExcel() { ExcelApp = new Application(); ExcelWorkbooks = ExcelApp.Workbooks; ExcelWorkbook = ExcelWorkbooks.Open(ExcelTemplatePath); ExcelSheet = ExcelWorkbook.Worksheets[1]; ExcelResources = new List <object>(); ExcelResources.AddRange(new object[] { ExcelApp, ExcelWorkbooks, ExcelWorkbook, ExcelSheet }); }
public ExcelWorkbook(string filename, string password = "") { ExcelApplication = new Application { DisplayAlerts = false }; ExcelWorkbooks = ExcelApplication.Workbooks; if (File.Exists(filename)) { try { Workbook = ExcelWorkbooks.Open(Filename: filename, UpdateLinks: false, ReadOnly: false, Password: password); } catch (Exception ex) { disposedValue = false; Dispose(true); throw new LocalSystemException("Could not open locked Excel file : " + filename, ex); } } else { try { Workbook = ExcelWorkbooks.Add(XlWBATemplate.xlWBATWorksheet); Save(filename, true, password); Workbook.Close(); Cleanup.ReleaseObject(Workbook); Workbook = ExcelWorkbooks.Open(Filename: xSaveName, UpdateLinks: false, ReadOnly: false, Password: password); } catch (Exception ex) { disposedValue = false; Dispose(true); throw new LocalSystemException("Could not create new Excel file : " + filename, ex); } } Worksheets = Workbook.Worksheets; }