c#中怎么将连接到数据库表格的dataGridView中的数据导出为excel表格文件
展开全部
先把datagridview的数据拷贝。
然后新建一个Excel文件。
复制到Excel文件。
private void copyAlltoClipboard()
{
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
private void btnExport_Click(object sender, EventArgs e)
{
copyAlltoClipboard();
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
展开全部
/// <summary>
/// 写入Excel文档
/// </summary>
/// <param name="Path">文件名称</param>
/// <param name="ds2Excel">被导出的数据集(dataGridView中的数据)</param>
public static void SaveFP2toExcel2(string Path, DataSet ds2Excel)
{
try
{
//创建Excel应用程序
Excel.Application xApp = new Excel.ApplicationClass();
if (xApp == null)
{
MessageBox.Show("错误:Excel不能打开!");
return;
}
object objOpt = Missing.Value;
Excel.Workbook xBook = xApp.Workbooks.Add(true);//添加新工作簿
Excel.Sheets xSheets = xBook.Sheets;
Excel._Worksheet xSheet = null;
//
//创建空的sheet
//
xSheet = (Excel._Worksheet)(xBook.Sheets.Add(objOpt, objOpt, objOpt, objOpt));
xSheet.Cells.NumberFormatLocal = "@";
if (xSheet == null)
{
MessageBox.Show("错误:工作表为空!");
return;
}
//写数据集表头
for (int k = 0; k < ds2Excel.Tables[0].Columns.Count; k++)
xSheet.Cells[1, k + 1] = ds2Excel.Tables[0].Columns[k].ColumnName.ToString().Trim();
//写数据集数据
for (int i = 0; i < ds2Excel.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds2Excel.Tables[0].Columns.Count; j++)
xSheet.Cells[i + 2, j + 1] = ds2Excel.Tables[0].Rows[i][j].ToString();
}
//保存文件
xBook.Saved = true;
xBook.SaveCopyAs(Path);
//显示文件
xApp.Visible = true;
//
//释放资源
//
Marshal.ReleaseComObject(xSheet);
xSheet = null;
Marshal.ReleaseComObject(xSheets);
xSheets = null;
Marshal.ReleaseComObject(xBook);
xBook = null;
xApp.Quit();
Marshal.ReleaseComObject(xApp);
xApp = null;
GC.Collect();//强行销毁
MessageBox.Show("导出成功");
}
catch (Exception ex)
{
MessageBox.Show("写入Excel发生错误:" + ex.Message);
}
}
/// 写入Excel文档
/// </summary>
/// <param name="Path">文件名称</param>
/// <param name="ds2Excel">被导出的数据集(dataGridView中的数据)</param>
public static void SaveFP2toExcel2(string Path, DataSet ds2Excel)
{
try
{
//创建Excel应用程序
Excel.Application xApp = new Excel.ApplicationClass();
if (xApp == null)
{
MessageBox.Show("错误:Excel不能打开!");
return;
}
object objOpt = Missing.Value;
Excel.Workbook xBook = xApp.Workbooks.Add(true);//添加新工作簿
Excel.Sheets xSheets = xBook.Sheets;
Excel._Worksheet xSheet = null;
//
//创建空的sheet
//
xSheet = (Excel._Worksheet)(xBook.Sheets.Add(objOpt, objOpt, objOpt, objOpt));
xSheet.Cells.NumberFormatLocal = "@";
if (xSheet == null)
{
MessageBox.Show("错误:工作表为空!");
return;
}
//写数据集表头
for (int k = 0; k < ds2Excel.Tables[0].Columns.Count; k++)
xSheet.Cells[1, k + 1] = ds2Excel.Tables[0].Columns[k].ColumnName.ToString().Trim();
//写数据集数据
for (int i = 0; i < ds2Excel.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds2Excel.Tables[0].Columns.Count; j++)
xSheet.Cells[i + 2, j + 1] = ds2Excel.Tables[0].Rows[i][j].ToString();
}
//保存文件
xBook.Saved = true;
xBook.SaveCopyAs(Path);
//显示文件
xApp.Visible = true;
//
//释放资源
//
Marshal.ReleaseComObject(xSheet);
xSheet = null;
Marshal.ReleaseComObject(xSheets);
xSheets = null;
Marshal.ReleaseComObject(xBook);
xBook = null;
xApp.Quit();
Marshal.ReleaseComObject(xApp);
xApp = null;
GC.Collect();//强行销毁
MessageBox.Show("导出成功");
}
catch (Exception ex)
{
MessageBox.Show("写入Excel发生错误:" + ex.Message);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询