execl导出问题,急
private void button1_Click(object sender, EventArgs e){
show();
}
private void show()
{
string strPath =Application.StartupPath + "\\788h.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:DataBase Password=4444;User Id=admin;Data source=" + strPath;;
OleDbConnection oleCon = new OleDbConnection(ConStr);
StringBuilder strSQL = new StringBuilder();
oleCon.Open();
OleDbDataAdapter oleDap = new OleDbDataAdapter(getsql(), oleCon);
DataSet ds = new DataSet();
oleDap.Fill(ds, "消费记录");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
oleCon.Close();
oleCon.Dispose();
}
private string getsql()
{
string sql = "select * from 消费记录 where id>=0 ";
if (textBox4.Text != null && textBox4.Text != "" && textBox5.Text != null && textBox5.Text != "")
sql += " and indate between #" + DateTime.Parse(textBox4.Text) + "# and #" + DateTime.Parse(textBox5.Text) + "#";
return sql += "order by id desc";
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
ExportDataGridview(dataGridView1, true);
}
public bool ExportDataGridview(DataGridView dgv, bool isShowExcle)
{
if (dgv.Rows.Count == 0)
return false;
//建立Excel对象
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成字段名称
for (int i = 0; i < dgv.ColumnCount; i++)
{
excel.Cells[1, i + 2] = dgv.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < dgv.RowCount; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString().Trim();//这一步在数据全部导出后出错
}
}
}
return true;
}
大家帮我看看,谢谢! --------------------编程问答-------------------- 没看出错,但是你都说了是数据全部导出后出错,那都导出完了,为什么还会再填充一次呢?用意是什么?
补充:.NET技术 , C#