问题描述
咨询一下各位大虾,我在做excel导入导出的时候,总数不成功,麻烦帮忙指教啊,谢谢了。代码如下:///<summary>///将DataSet数据导出到Excel,DataSet数据从数据库读取///</summary>///<paramname="ds">DataSet数据源</param>publicvoidExportToExcel(DataSetds){//TbBindData是一个集合,通过excel导入进来intintCnttCount=ds.Tables[0].AsEnumerable().GroupBy(x=>x.Field<string>("合同编号").ToUpper()).Count();//根据合同编号查询有多少相同的合同if(intCnttCount>1)//此处判断若有重复记录,则不能导入(大于1则表示有重复的合同){//表格中存在不同的合同编号,则返回,不导入,请核实数据后重新导入return;}if(ds==null){thrownewArgumentNullException("导出数据源为空!argument:ds");}HttpResponseresponse=HttpContext.Current.Response;this.AddResponseHeader(response);StringBuilderresponseXMLInfo=newStringBuilder();responseXMLInfo.Append(this.CreateXmlHeader());responseXMLInfo.Append(@"<Workbookxmlns='urn:schemas-microsoft-com:office:spreadsheet'xmlns:o='urn:schemas-microsoft-com:office:office'xmlns:x='urn:schemas-microsoft-com:office:excel'xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'xmlns:html='http://www.w3.org/TR/REC-html40'>");responseXMLInfo.Append(this.CreateDocumentProperties());responseXMLInfo.Append(this.CreateStyle());foreach(DataTabledtinds.Tables){responseXMLInfo.Append(this.CreateSheet(dt));}responseXMLInfo.Append("</Workbook>");response.Write(responseXMLInfo);response.Flush();response.End();}---------------///<summary>///预览导入的Excel文件,将excel数据读取到Gridview///</summary>///<paramname="sender"></param>///<paramname="e"></param>protectedvoidbtnPriview_Click(objectsender,EventArgse){this.lblErr.Text=string.Empty;if(!string.IsNullOrEmpty(filePath.Value.Trim())){if(filePath.Value.Trim().Substring(filePath.Value.Trim().LastIndexOf("."))==".xls"){imspc.FrameWork.FileUploadupload=newimspc.FrameWork.FileUpload(filePath);stringsNewFileName=string.Empty;upload.FilePath=filePath.Value.Trim();sNewFileName=upload.NewFileUp();ExcelHelperexHlp=newExcelHelper();exHlp.FilePathStr=Server.MapPath("~/UploadFile/"+sNewFileName);//SetfilepathexHlp.index=0;//setexcelindexexHlp.Open();DataSetaDs=exHlp.GetExcelData();//Getdatasourcestring[]strTemp={"合同编号","机组","子项代码","系统","实际完成日期","备注"};if(exHlp.ValidateExcelTemlete(strTemp))//判断模板是否匹配{this.btnImport.Enabled=true;DataTabletbExcel=aDs.Tables[0];this.TbBindData=tbExcel;this.dgList.DataSource=tbExcel;//binddatagridthis.dgList.DataBind();}}else{this.ClientScript.RegisterClientScriptBlock(typeof(string),"key","alert('模板出错!')",true);}}}