日期:2014-05-18 浏览次数:21024 次
#region//把datagridview中的数据全部导出到EXCEL
/// <summary>
/// 把datagridview中的数据全部导出到EXCEL
/// </summary>
/// <param name="dg1"></param>
public static void CopyToExcel(DataGridView dg1)
{
dg1.SelectAll();
Application.DoEvents();
Clipboard.SetText(dg1.GetClipboardContent().GetData(DataFormats.Text).ToString());
object oMissing = System.Reflection.Missing.Value;
try
{
Excel._Worksheet Sht;
Excel._Workbook Bo;
Excel.Application excel = new Excel.Application();
Bo = excel.Application.Workbooks.Add(true);
Sht = (Excel.Worksheet)Bo.Sheets[1];
Sht.Paste(oMissing, oMissing);
excel.Visible = true;
excel = null;
}
catch (Exception ex)
{
ex.ToString();
}
finally
{
}
}
#endregion
#region//把datagridview中选择的数据全部导出到EXCEL
/// <summary>
/// 把datagridview中选择的数据全部导出到EXCEL
/// </summary>
/// <param name="dg1"></param>
public static void CopyToExcelSelect(DataGridView dg1)
{
if (dg1.Rows.Count < 1)
return;
Clipboard.SetText(dg1.GetClipboardContent().GetData(DataFormats.Text).ToString());
object oMissing = System.Reflection.Missing.Value;
try
{
Excel._Worksheet Sht;
Excel._Workbook Bo;
Excel.Application excel = new Excel.Application();
Bo = excel.Application.Workbooks.Add(true);
Sht = (Excel.Worksheet)Bo.Sheets[1];
Sht.Paste(oMissing, oMissing);
excel.Visible = true;
excel = null;
}
catch (Exception ex)
{
ex.ToString();
}
finally
{
}
}
#endregion