internal static void ExportExcel(string name, CustomDataGridView grid) { string fName = GetFileName(name); if (fName == null) return; try { DoExport(fName, grid); MessageBox.Show(null, "Export finished!", "Excel export", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(null, string.Format("Error during export: {0}", ex.Message), "Excel export", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private static void DoExport(string fName, CustomDataGridView grid) { ExcelPackage ep = new ExcelPackage(); var ws = ep.Workbook.Worksheets.Add("Exported data"); // hlavicka var col = 1; for (int i = 0; i < grid.Columns.Count; i++) { // len zobrazene stlpce if (!grid.Columns[i].Visible) continue; ws.Cells[1, col].Value = grid.Columns[i].HeaderText; ws.Cells[1, col].Style.Font.Bold = true; ws.Cells[1, col].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; ws.Cells[1, col].Style.Fill.BackgroundColor.SetColor(Color.GreenYellow); ws.Cells[1, col].Style.Border.BorderAround(ExcelBorderStyle.Medium, Color.DarkGray); ws.Column(col).Width = 17; var row = 2; for (int j = 0; j < grid.Rows.Count; j++) { var obj = grid[i, j].Value; // farbicky pre parne riadky if (obj != null) { /*var dp = GetPrice(obj.ToString()); if (!double.IsNaN(dp)) ws.Cells[row, col].Value = dp; else*/ ws.Cells[row, col].Value = obj.ToString(); } if (row % 2 == 1) { ws.Cells[row, col].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; ws.Cells[row, col].Style.Fill.BackgroundColor.SetColor(Color.LemonChiffon); } ws.Cells[row, col].Style.Border.Left.Style = ExcelBorderStyle.Thin; ws.Cells[row, col].Style.Border.Left.Color.SetColor(Color.DarkGray); ws.Cells[row, col].Style.Border.Right.Style = ExcelBorderStyle.Thin; ws.Cells[row, col].Style.Border.Right.Color.SetColor(Color.DarkGray); row++; } col++; } // ulozenie ep.SaveAs(new FileInfo(fName)); }
private List<WaitingProductEntity> GetSelectedWaitingEnts(CustomDataGridView grid) { var sel = grid.SelectedCells; var ret = new List<WaitingProductEntity>(); for (int i = 0; i < sel.Count; i++) { var rowi = sel[i].RowIndex; var item = grid.Rows[rowi].DataBoundItem as WaitingProductEntity; ret.Add(item); } return ret; }