private void отобразитьВExcelToolStripMenuItem_Click(object sender, EventArgs e) { ExcelClass InformationAboutElements = new ExcelClass(); Font HeadFont = new Font(" Times New Roman ", 12.0f, FontStyle.Bold); try { int CurrentCell=0; InformationAboutElements.NewDocument(); InformationAboutElements.AddNewPageAtTheStart("Параметры элемента УСП"); InformationAboutElements.SelectCells("A1", Type.Missing); InformationAboutElements.SetColumnWidth(20); InformationAboutElements.SetFont(HeadFont, 0); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick); InformationAboutElements.WriteDataToCell("Параметр"); InformationAboutElements.SelectCells("B1", Type.Missing); InformationAboutElements.SetColumnWidth(30); InformationAboutElements.SetFont(HeadFont, 0); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick); InformationAboutElements.WriteDataToCell("Значение"); for (int i = 0; i < DataInformTable.RowCount; i++) { CurrentCell = i + 2; InformationAboutElements.SelectCells(("A" + CurrentCell.ToString()), Type.Missing); InformationAboutElements.WriteDataToCell(DataInformTable[0,i].Value.ToString()); InformationAboutElements.SetHorisontalAlignment(2); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick); InformationAboutElements.SelectCells(("B" + CurrentCell.ToString()), Type.Missing); InformationAboutElements.WriteDataToCell(DataInformTable[1, i].Value.ToString()); InformationAboutElements.SetHorisontalAlignment(2); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick); } } catch (Exception ex) { MessageBox.Show(ex.Message,"Error"); } finally { InformationAboutElements.Visible = true; InformationAboutElements.Dispose(); HeadFont.Dispose(); } }
private void показатьВExcelToolStripMenuItem_Click(object sender, EventArgs e) { ExcelClass InformationAboutElements = new ExcelClass(); Font HeadFont = new Font(" Times New Roman ", 12.0f, FontStyle.Bold); try { string currentName = ""; int countCell = 2; InformationAboutElements.NewDocument(); InformationAboutElements.AddNewPageAtTheStart("Параметры элемента УСП"); InformationAboutElements.SelectCells("A1", Type.Missing); InformationAboutElements.SetColumnWidth(20); InformationAboutElements.SetFont(HeadFont, 0); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick); InformationAboutElements.WriteDataToCell("Параметр"); InformationAboutElements.SelectCells("B1", Type.Missing); InformationAboutElements.SetColumnWidth(30); InformationAboutElements.SetFont(HeadFont, 0); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick); InformationAboutElements.WriteDataToCell("Значение"); for (int i = 1; i < 28; i++) { for (int j = 0; j < this.panel1.Controls.Count; j++) { if ((String.Compare(this.panel1.Controls[j].Tag.ToString(), i.ToString()) == 0)) { if ((String.Compare(this.panel1.Controls[j].Text, "") != 0)&&(String.Compare(this.panel1.Controls[j].Text, "0") != 0)) { currentName = ((System.Windows.Forms.Label)(this.Controls.Find(("label" + i.ToString()), true)[0])).Text; InformationAboutElements.SelectCells((string)(("B" + countCell.ToString())), Type.Missing); InformationAboutElements.WriteDataToCell(this.panel1.Controls[j].Text); InformationAboutElements.SetHorisontalAlignment(2); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick); InformationAboutElements.SelectCells((string)(("A" + countCell.ToString())), Type.Missing); InformationAboutElements.WriteDataToCell(currentName); InformationAboutElements.SetHorisontalAlignment(2); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick); countCell++; } } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } finally { InformationAboutElements.Visible = true; InformationAboutElements.Dispose(); HeadFont.Dispose(); } }
void ExportDGVToExcel(DataGridView dgv) { ExcelClass InformationAboutElements = new ExcelClass(); Font HeadFont = new Font(" Times New Roman ", 12.0f, FontStyle.Bold); int iterator = 0; try { char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); int CurrentCell = 0; InformationAboutElements.NewDocument(); InformationAboutElements.AddNewPageAtTheStart("Данные"); for (int i = 0; i < dgv.Columns.Count; i++) { if (dgv.Columns[i].Visible == true) { InformationAboutElements.SelectCells(alpha[iterator] + (1).ToString(), Type.Missing); InformationAboutElements.SetFont(HeadFont, 0); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin); InformationAboutElements.WriteDataToCell(dgv.Columns[i].HeaderText); for (int j = 0; j < dgv.Rows.Count; j++) { InformationAboutElements.SelectCells(alpha[iterator] + (j + 2).ToString(), Type.Missing); InformationAboutElements.SetFont(HeadFont, 0); InformationAboutElements.SetBorderStyle(0, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin); InformationAboutElements.setAutoFit(alpha[iterator] + (j + 2).ToString()); InformationAboutElements.WriteDataToCell(dgv[i, j].Value.ToString()); } if (dgv[i, 0].Value.ToString().Length > dgv.Columns[i].HeaderText.Length) { InformationAboutElements.setAutoFit(alpha[iterator] + (2).ToString()); } else { InformationAboutElements.setAutoFit(alpha[iterator] + (1).ToString()); } iterator++; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } finally { InformationAboutElements.Visible = true; InformationAboutElements.Dispose(); HeadFont.Dispose(); } }