private void onOpenFolder(object sender, EventArgs e) { FolderBrowserDialog folderDlg = new FolderBrowserDialog(); DialogResult result = folderDlg.ShowDialog(); if (result == DialogResult.OK) { new Thread(() => new loading_form().ShowDialog()).Start(); getStatsInFolder(folderDlg.SelectedPath); for (int j = 0; j < max_column_count; j++) { this.viewcali.Columns[j].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; } loading_form f = new loading_form(); f = (loading_form)Application.OpenForms["loading_form"]; f.Close(); } }
private void importFromXML(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Camera File|*.xlsx"; openFileDialog1.Title = "Select a excel Log File"; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { new Thread(() => new loading_form().ShowDialog()).Start(); Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); Excel.Workbook workbook = app.Workbooks.Open(openFileDialog1.FileName); Excel.Worksheet worksheet = workbook.ActiveSheet; //this.viewcali.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; int rcount = worksheet.UsedRange.Rows.Count; int i = 1; //this.viewcali.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; for (; i < rcount; i++) { //dataGridView1.Rows[i].Cells["Column1"].Value = worksheet.Cells[i + 1, 1].Value; //dataGridView1.Rows[i].Cells["Column2"].Value = worksheet.Cells[i + 1, 2].Value; /*this.viewcali.Rows.Add(worksheet.Cells[i + 1, 1].Value, worksheet.Cells[i + 1, 2].Value, * worksheet.Cells[i + 1, 3].Value, * worksheet.Cells[i + 1, 4].Value, * worksheet.Cells[i + 1, 5].Value, * worksheet.Cells[i + 1, 6].Value, * worksheet.Cells[i + 1, 7].Value, * worksheet.Cells[i + 1, 8].Value, * worksheet.Cells[i + 1, 9].Value, * worksheet.Cells[i + 1, 10].Value, * worksheet.Cells[i + 1, 11].Value, * worksheet.Cells[i + 1, 12].Value, * worksheet.Cells[i + 1, 13].Value, * worksheet.Cells[i + 1, 14].Value, * worksheet.Cells[i + 1, 15].Value, * worksheet.Cells[i + 1, 16].Value);*/ DataRow newRow = cali_dt.NewRow(); newRow[0] = worksheet.Cells[i + 1, 1].Value; newRow[1] = worksheet.Cells[i + 1, 2].Value; newRow[2] = worksheet.Cells[i + 1, 3].Value; newRow[3] = worksheet.Cells[i + 1, 4].Value; newRow[4] = worksheet.Cells[i + 1, 5].Value; newRow[5] = worksheet.Cells[i + 1, 6].Value; newRow[6] = worksheet.Cells[i + 1, 7].Value; newRow[7] = worksheet.Cells[i + 1, 8].Value; newRow[8] = worksheet.Cells[i + 1, 9].Value; newRow[9] = worksheet.Cells[i + 1, 10].Value; newRow[10] = worksheet.Cells[i + 1, 11].Value; newRow[11] = worksheet.Cells[i + 1, 12].Value; newRow[12] = worksheet.Cells[i + 1, 13].Value; newRow[13] = worksheet.Cells[i + 1, 14].Value; newRow[14] = worksheet.Cells[i + 1, 15].Value; newRow[15] = worksheet.Cells[i + 1, 16].Value; newRow[16] = worksheet.Cells[i + 1, 17].Value; cali_dt.Rows.Add(newRow); this.viewcali.DataSource = cali_dt; } workbook.Close(); for (int j = 0; j < max_column_count; j++) { this.viewcali.Columns[j].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; } loading_form f = new loading_form(); f = (loading_form)Application.OpenForms["loading_form"]; f.Close(); } }
public void ExportToExcelWithFormatting(DataGridView dataGridView1) { string fileName; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "xls files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; saveFileDialog1.Title = "To Excel"; saveFileDialog1.FileName = this.Text + " (" + DateTime.Now.ToString("yyyy-MM-dd") + ")"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { new Thread(() => new loading_form().ShowDialog()).Start(); fileName = saveFileDialog1.FileName; var workbook = new XLWorkbook(); var worksheet = workbook.Worksheets.Add(this.Text); for (int i = 0; i < dataGridView1.Columns.Count; i++) { worksheet.Cell(1, i + 1).Value = dataGridView1.Columns[i].Name; } Debug.WriteLine(dataGridView1.Rows.Count); Debug.WriteLine(dataGridView1.Columns.Count); for (int i = 0; i < dataGridView1.Rows.Count; i++) { for (int j = 0; j < dataGridView1.Columns.Count; j++) { Debug.WriteLine(dataGridView1.Rows[i].Cells[j].Value.ToString()); worksheet.Cell(i + 2, j + 1).Value = dataGridView1.Rows[i].Cells[j].Value.ToString(); if (worksheet.Cell(i + 2, j + 1).Value.ToString().Length > 0) { XLAlignmentHorizontalValues align; switch (dataGridView1.Rows[i].Cells[j].Style.Alignment) { case DataGridViewContentAlignment.BottomRight: align = XLAlignmentHorizontalValues.Right; break; case DataGridViewContentAlignment.MiddleRight: align = XLAlignmentHorizontalValues.Right; break; case DataGridViewContentAlignment.TopRight: align = XLAlignmentHorizontalValues.Right; break; case DataGridViewContentAlignment.BottomCenter: align = XLAlignmentHorizontalValues.Center; break; case DataGridViewContentAlignment.MiddleCenter: align = XLAlignmentHorizontalValues.Center; break; case DataGridViewContentAlignment.TopCenter: align = XLAlignmentHorizontalValues.Center; break; default: align = XLAlignmentHorizontalValues.Left; break; } worksheet.Cell(i + 2, j + 1).Style.Alignment.Horizontal = align; XLColor xlColor = XLColor.FromColor(dataGridView1.Rows[i].Cells[j].Style.SelectionBackColor); worksheet.Cell(i + 2, j + 1).AddConditionalFormat().WhenLessThan(1).Fill.SetBackgroundColor(xlColor); worksheet.Cell(i + 2, j + 1).Style.Font.FontName = dataGridView1.Font.Name; worksheet.Cell(i + 2, j + 1).Style.Font.FontSize = dataGridView1.Font.Size; } } } worksheet.Columns().AdjustToContents(); workbook.SaveAs(fileName); loading_form f = new loading_form(); f = (loading_form)Application.OpenForms["loading_form"]; f.Close(); //MessageBox.Show("Done"); } }