C#, 把access数据导出成excel表格
1个回答
展开全部
添加一个引用(micros excel),然后添加如下代码:
Microsoft.Office.Interop.Excel.ApplicationClass myexcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
myexcel.Visible = true;
if (myexcel == null)
{
MessageBox.Show("无法启动excel");
return;
}
myexcel.Application.Workbooks.Add(true);
int rowscount = this.dataGridView1.RowCount;
int colcount = this.dataGridView1.ColumnCount;
for (int i = 0; i < colcount; i++)
{
myexcel.Cells[1, i + 1] = this.dataGridView1.Columns[i].HeaderText;
}
for (int i = 0; i < rowscount; i++)
{
for (int j = 0; j < colcount; j++)
{
myexcel.Cells[i + 2, j + 1] = this.dataGridView1[j,i].Value.ToString();
}
}
Microsoft.Office.Interop.Excel.ApplicationClass myexcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
myexcel.Visible = true;
if (myexcel == null)
{
MessageBox.Show("无法启动excel");
return;
}
myexcel.Application.Workbooks.Add(true);
int rowscount = this.dataGridView1.RowCount;
int colcount = this.dataGridView1.ColumnCount;
for (int i = 0; i < colcount; i++)
{
myexcel.Cells[1, i + 1] = this.dataGridView1.Columns[i].HeaderText;
}
for (int i = 0; i < rowscount; i++)
{
for (int j = 0; j < colcount; j++)
{
myexcel.Cells[i + 2, j + 1] = this.dataGridView1[j,i].Value.ToString();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询