Good afternoon I tell you I would like to export the data that the program throws to an excel but I do not get it, when I open the excel no data appear, here I leave my code ...
// method to save the data in excel
private void button14_Click(object sender, EventArgs e)
{
saveExcel(progressBar1, grd);
}
private void saveExcel(ProgressBar pBar, DataGridView grd)
{
SaveFileDialog fichero = new SaveFileDialog();
fichero.Filter = "Excel (*.xls)|*.xls";
if (pBar != null)
{
pBar.Maximum = grd.RowCount;
pBar.Value = 0;
if (!pBar.Visible) pBar.Visible = true;
}
if (fichero.ShowDialog() == DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application aplicacion;
Microsoft.Office.Interop.Excel.Workbook libros_trabajo;
Microsoft.Office.Interop.Excel.Worksheet hoja_trabajo;
aplicacion = new Microsoft.Office.Interop.Excel.Application();
libros_trabajo = aplicacion.Workbooks.Add();
hoja_trabajo =
(Microsoft.Office.Interop.Excel.Worksheet)libros_trabajo.Worksheets.get_Item(1);
//Recorremos el DataGridView rellenando la hoja de trabajo
for (int i = 0; i < grd.Rows.Count - 1; i++)
{
for (int j = 0; j < grd.Columns.Count; j++)
{
hoja_trabajo.Cells[i + 1, j + 1] = grd.Rows[i].Cells[j].Value.ToString();
}
pBar.Value += 1;
}
libros_trabajo.SaveAs(fichero.FileName,
Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal);
libros_trabajo.Close(true);
aplicacion.Quit();
}
if (pBar != null)
{
pBar.Value = 0;
pBar.Visible = false;
MessageBox.Show("Datos exportados correctamente");
}
}
// method to extract processor information
DataGridView grd = new DataGridView();
private void MuestraInformacion(String Key)
{
listView1.Items.Clear();
listView1.View = View.Details;
listView1.Columns.Add("Propiedad");
listView1.Columns.Add("Valor");
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);
foreach (ManagementObject objeto in searcher.Get())
{
string grupo = objeto["Name"].ToString();
listView1.Groups.Add(grupo, grupo);
if (objeto.Properties.Count <= 0)
{
MessageBox.Show("La Información No Está Disponible", "No Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
ListViewItem listViewItem1 = null;
foreach (PropertyData PropiedadObjeto in objeto.Properties)
{
listViewItem1 = new ListViewItem(PropiedadObjeto.Name);
if (PropiedadObjeto.Value != null && PropiedadObjeto.Value.ToString() != "")
{
listViewItem1.SubItems.Add(PropiedadObjeto.Value.ToString());
}
listView1.Items.Add(listViewItem1);
listView1.Groups[grupo].Items.Add(listViewItem1);
}
grd.DataSource = listViewItem1;
}
// method for the button
private void button14_Click(object sender, EventArgs e)
{
saveExcel(progressBar1, grd);
}