private void SaveFileDialog_Click(object sender, EventArgs e) { try { ExcelSaveFileDialog.FileName = "newfile.xlsx"; ExcelSaveFileDialog.Filter = "Excelブック(*.xls;*.xlsx)|*.xls;*.xlsx"; ExcelSaveFileDialog.Title = "Excelファイルの保存先を選択してください。"; ExcelSaveFileDialog.OverwritePrompt = false; ExcelSaveFileDialog.CheckFileExists = false; if (ExcelSaveFileDialog.ShowDialog() == DialogResult.OK) { ExcelSaveFilePathTextBox.Text = ExcelSaveFileDialog.FileName; } } catch (Exception ex) { throw Program.ThrowException(ex); } }
private void ExportGridToExcel() { ExcelSaveFileDialog.FileName = string.Format( "{0}.{1}-{2:yyyyMMdd}-{2:HHmmss}.xlsx", indexDataProvider.GetServerName(), indexDataProvider.GetDatabaseName(), DateTime.Now); if (ExcelSaveFileDialog.ShowDialog(this) != DialogResult.OK) { return; } Cursor = Cursors.WaitCursor; try { if (File.Exists(ExcelSaveFileDialog.FileName)) { File.Delete(ExcelSaveFileDialog.FileName); } using (var wb = new XLWorkbook()) using (var ws = wb.Worksheets.Add("SqlIndexConsole")) { ws.Cells("A1").Value = "Reorganize"; ws.Cells("B1").Value = "Rebuild"; ws.Cells("C1").Value = "Schema"; ws.Cells("D1").Value = "Object"; ws.Cells("E1").Value = "Index"; ws.Cells("F1").Value = "Type"; ws.Cells("G1").Value = "Size"; ws.Cells("H1").Value = "Pages"; ws.Cells("I1").Value = "Frag %"; ws.Cells("J1").Value = "Recommendation"; ws.Cells("K1").Value = "Reason"; ws.Cells("L1").Value = "Columns"; ws.Cells("M1").Value = "Include"; ws.Cells("N1").Value = "Filter"; ws.Cells("O1").Value = "Fill Factor"; ws.Cells("P1").Value = "Unique"; ws.Cells("Q1").Value = "PK"; ws.Cells("R1").Value = "Unique Constraint"; ws.Cells("S1").Value = "Data Space"; int i = 2; foreach (DataGridViewRow row in IndexDataGridView.Rows) { var ivm = row.DataBoundItem as IndexViewModel; ws.Cell(string.Concat("A", i)).Value = ivm.Reorganize ? "Y" : string.Empty; ws.Cell(string.Concat("B", i)).Value = ivm.Rebuild ? "Y" : string.Empty; ws.Cell(string.Concat("C", i)).Value = ivm.SchemaName; ws.Cell(string.Concat("D", i)).Value = ivm.ObjectName; ws.Cell(string.Concat("E", i)).Value = ivm.IndexName; ws.Cell(string.Concat("F", i)).Value = ivm.IndexTypeDescription; ws.Cell(string.Concat("G", i)).Value = ivm.SizeDescription; ws.Cell(string.Concat("H", i)).Value = ivm.PageCount; ws.Cell(string.Concat("I", i)).Value = ivm.AvgFragmentationPercent; ws.Cell(string.Concat("J", i)).Value = ivm.RecommendationDescription; ws.Cell(string.Concat("K", i)).Value = ivm.RecommendationReason; ws.Cell(string.Concat("L", i)).Value = ivm.ColumnsDescription; ws.Cell(string.Concat("M", i)).Value = ivm.IncludeColumnsDescription; ws.Cell(string.Concat("N", i)).Value = ivm.FilterDefinition; ws.Cell(string.Concat("O", i)).Value = ivm.FillFactor; ws.Cell(string.Concat("P", i)).Value = ivm.Index.IsUnique ? "Y" : string.Empty; ws.Cell(string.Concat("Q", i)).Value = ivm.Index.IsPrimaryKey ? "Y" : string.Empty; ws.Cell(string.Concat("R", i)).Value = ivm.Index.IsUniqueConstraint ? "Y" : string.Empty; ws.Cell(string.Concat("S", i)).Value = ivm.DataSpaceName; i++; } wb.SaveAs(ExcelSaveFileDialog.FileName); } } finally { Cursor = Cursors.Default; } }