O
OscarMolleapaza
Guest
private bool DataGridviewImportToExcel(DataGridView[] dgv, string fileName)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel file|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0)
return false;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("can not create Excel");
return false;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
for (int index = 0; index < dgv.Length; index++)
{
string[] sheetsCalidad = new string[]{ "TempZonaRec", "TempCalentamiento","TempHomogenizacion","TiemDeshornamiento","TempAustenizacion","TempConformado","TempTemple","FlujoAguaTamb2", "FlujoAguaTamb3","TempAguaTamb1","TempAguaTamb2","VelTamb2","VelTamb3","VelMesEnfr2","TempEcuLadDer","TempSalMesEnfr2" };
for (int i = 0; i < dgv[index].ColumnCount; i++)
{
worksheet.Cells[1, i + 1] = dgv[index].Columns.HeaderText;
}
for (int r = 0; r < dgv[index].Rows.Count; r++)
{
for (int i = 0; i < dgv[index].ColumnCount; i++)
{
worksheet.Cells[r + 2, i + 1] = dgv[index].Rows[r].Cells.Value.ToString();
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();
worksheet.Name = sheetsCalidad[index];
if (index < dgv.Length - 1)
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add();
}
}
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
}
catch (Exception ex)
{
MessageBox.Show("error,file maybe is opening!\n" + ex.Message);
return false;
}
}
xlApp.Quit();
GC.Collect();
MessageBox.Show("File: " + fileName + ".xls save Successfully", "tip ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
Continue reading...
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel file|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0)
return false;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("can not create Excel");
return false;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
for (int index = 0; index < dgv.Length; index++)
{
string[] sheetsCalidad = new string[]{ "TempZonaRec", "TempCalentamiento","TempHomogenizacion","TiemDeshornamiento","TempAustenizacion","TempConformado","TempTemple","FlujoAguaTamb2", "FlujoAguaTamb3","TempAguaTamb1","TempAguaTamb2","VelTamb2","VelTamb3","VelMesEnfr2","TempEcuLadDer","TempSalMesEnfr2" };
for (int i = 0; i < dgv[index].ColumnCount; i++)
{
worksheet.Cells[1, i + 1] = dgv[index].Columns.HeaderText;
}
for (int r = 0; r < dgv[index].Rows.Count; r++)
{
for (int i = 0; i < dgv[index].ColumnCount; i++)
{
worksheet.Cells[r + 2, i + 1] = dgv[index].Rows[r].Cells.Value.ToString();
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();
worksheet.Name = sheetsCalidad[index];
if (index < dgv.Length - 1)
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add();
}
}
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
}
catch (Exception ex)
{
MessageBox.Show("error,file maybe is opening!\n" + ex.Message);
return false;
}
}
xlApp.Quit();
GC.Collect();
MessageBox.Show("File: " + fileName + ".xls save Successfully", "tip ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
Continue reading...