/// <summary> /// </summary> /// <param name="filename"></param> private void ExportToExcel(string filename) { using (FileStream fs = new FileStream(AppUtility.GetAppPath() + @"templates\prizewinner.xls", FileMode.Open, FileAccess.Read, FileShare.Read)) { IWorkbook workBook = new HSSFWorkbook(fs); ISheet sheet = workBook.GetSheetAt(0); sheet.GetRow(1).GetCell(1).SetCellValue(cbxGroup.SelectedItem.ToString()); int i = 3; foreach (DataGridViewRow row in dgvPrizewinnerList.Rows) { IRow erow = sheet.CreateRow(i++); erow.CreateCell(0).SetCellValue(row.Cells["txtEmpId"].Value.ToString()); erow.CreateCell(1).SetCellValue(row.Cells["txtName"].Value.ToString()); erow.CreateCell(2).SetCellValue(row.Cells["txtDept"].Value.ToString()); erow.CreateCell(3).SetCellValue(row.Cells["txtPost"].Value.ToString()); erow.CreateCell(4).SetCellValue(row.Cells["txtAward"].Value.ToString()); erow.CreateCell(5).SetCellValue(row.Cells["boolsign"].Value.ToString()); } using (FileStream fs2 = new FileStream(filename, FileMode.Create, FileAccess.Write)) { workBook.Write(fs2); fs2.Flush(); } } }