winform中datagrid 如何转换excel
winform中datagrid 如何转换excel
------解决方案--------------------private void button3_Click(object sender, System.EventArgs e)
{
//导出数据到Excel
if(this.dataSet11.Tables[0].Rows.Count==0)
{
MessageBox.Show("没有可以导出的数据!","提示");
}
else
{
string strFileName="";
DialogResult r= saveFileDialog1.ShowDialog(this);
if(r == DialogResult.OK)
{
strFileName =saveFileDialog1.FileName ;
}
else
{
return;
}
//this.Cursor= Cursor.
Excel.Application app= new Excel.ApplicationClass();
Excel.Workbook mybook =null;
if(app.Workbooks.Count >0)
{
mybook = app.Workbooks[1];
}
else
{
mybook=app.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
}
Excel.Worksheet mysheet =null;
if(mybook.Worksheets.Count==0)
{
mysheet = (Excel.Worksheet)mybook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
else
{
mysheet = (Excel.Worksheet)mybook.Worksheets[1];
}
for(int i=0;i<dataSet11.Tables[0].Columns.Count;i++)
{
mysheet.Cells[1,i+1] = dataSet11.Tables[0].Columns[i].ColumnName ;
}
for(int i=0;i < dataSet11.Tables[0].Rows.Count ;i++)
{
DataRow dr = dataSet11.Tables[0].Rows[i];
for (int j=0;j<dataSet11.Tables[0].Columns.Count ;j++)
{
mysheet.Cells[i+2,j+1]= dr[j].ToString();
}
}
mybook.Close(true,strFileName,null);
mysheet = null;
mybook = null;
app.Quit ();
app = null;
MessageBox.Show("数据导出完毕!共导出"+this.dataSet11.Tables[0].Rows.Count+"条记录","提示");
}
}
------解决方案--------------------for 读格子,
然后用excel的如Interop.Excel.dll等控件写excel.
------解决方案--------------------这儿不是有吗?唉浪费啊,你需要什么你就先找看看,找不到再发帖嘛
http://topic.csdn.net/u/20070927/17/27eb9bfe-5cf3-4d0d-83bd-5a10f2ab6de7.html
------解决方案--------------------在引用中添加Microsoft Excel这个COM组件
然后用oledbconnection连接excel
------解决方案--------------------http://www.codeproject.com/office/office_automation.asp