问题描述
privatevoidbutton1_Click(objectsender,EventArgse){show();}privatevoidshow(){stringstrPath=Application.StartupPath+"\788h.mdb";stringConStr="Provider=Microsoft.Jet.OLEDB.4.0;JetOLEDB:DataBasePassword=4444;UserId=admin;Datasource="+strPath;;OleDbConnectionoleCon=newOleDbConnection(ConStr);StringBuilderstrSQL=newStringBuilder();oleCon.Open();OleDbDataAdapteroleDap=newOleDbDataAdapter(getsql(),oleCon);DataSetds=newDataSet();oleDap.Fill(ds,"消费记录");this.dataGridView1.DataSource=ds.Tables[0].DefaultView;oleCon.Close();oleCon.Dispose();}privatestringgetsql(){stringsql="select*from消费记录whereid>=0";if(textBox4.Text!=null&&textBox4.Text!=""&&textBox5.Text!=null&&textBox5.Text!="")sql+="andindatebetween#"+DateTime.Parse(textBox4.Text)+"#and#"+DateTime.Parse(textBox5.Text)+"#";returnsql+="orderbyiddesc";}privatevoidlinkLabel1_LinkClicked(objectsender,LinkLabelLinkClickedEventArgse){ExportDataGridview(dataGridView1,true);}publicboolExportDataGridview(DataGridViewdgv,boolisShowExcle){if(dgv.Rows.Count==0)returnfalse;//建立Excel对象Excel.Applicationexcel=newExcel.Application();excel.Application.Workbooks.Add(true);excel.Visible=isShowExcle;//生成字段名称for(inti=0;i<dgv.ColumnCount;i++){excel.Cells[1,i+2]=dgv.Columns[i].HeaderText;}//填充数据for(inti=0;i<dgv.RowCount;i++){for(intj=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();//这一步在数据全部导出后出错}}}returntrue;}大家帮我看看,谢谢!
解决方案
解决方案二:
没看出错,但是你都说了是数据全部导出后出错,那都导出完了,为什么还会再填充一次呢?用意是什么?