问题描述
导出excel提示错误cellRowvalue65536mustbebetween1and65534///<summary>///创建一个Excel文件(MyXls)///</summary>///<paramname="strPath">文件路径</param>///<paramname="strFileName">文件名</param>///<paramname="dtClolumn">列定义</param>///<paramname="dtData">数据</param>publicstaticvoidCreateExcelFile(stringstrPath,stringstrFileName,DataTabledtClolumn,DataTabledtData){XlsDocumentdoc=newXlsDocument();doc.DocumentSummaryInformation.Company="ZHXSOFTWARE";doc.FileName=strFileName;Worksheetsheet=doc.Workbook.Worksheets.Add("Data");org.in2bits.MyXls.Cellcell;XFcellXF=doc.NewXF();cellXF.VerticalAlignment=VerticalAlignments.Centered;cellXF.HorizontalAlignment=org.in2bits.MyXls.HorizontalAlignments.Centered;cellXF.Font.FontName="Arial";//cellXF.Font.Height=48*20;cellXF.Font.Bold=false;cellXF.Pattern=1;//设定单元格填充风格。如果设定为0,则是纯色填充(无色),1代表没有间隙的实色cellXF.PatternColor=Colors.Grey;//设定填充线条的颜色cellXF.PatternBackgroundColor=Colors.Grey;//填充的底色intn;DataViewdvClolumn=dtClolumn.DefaultView;dvClolumn.RowFilter="isnull(groupname,'')<>''";//多抬头处理if(dvClolumn.Count>0){n=1;for(inti=0;i<dtClolumn.Rows.Count;i++){cell=sheet.Cells.Add(1,i+1,dtClolumn.Rows[i]["Caption"].ToString(),cellXF);}stringgroupname="";stringold_groupname="";for(inti=0;i<dtClolumn.Rows.Count;i++){groupname=dtClolumn.Rows[i]["groupname"].ToString();//左右单元格合并if(groupname!=""){if(old_groupname!=groupname){dvClolumn.RowFilter="groupname='"+groupname+"'";intj=dvClolumn.Count+i;MergeAreameaa=newMergeArea(1,1,i+1,j);//一个合并单元格实例(合并第1行、第2例到第5行、第7例)sheet.AddMergeArea(meaa);//填加合并单元格cell=sheet.Cells.Add(1,i+1,groupname);i=j-1;}old_groupname=groupname;}else{//上下单元格合并MergeAreameaa=newMergeArea(1,2,i+1,i+1);sheet.AddMergeArea(meaa);cell=sheet.Cells.Add(1,i+1,dtClolumn.Rows[i]["Caption"].ToString());}}}else{n=0;}//列头定义for(inti=0;i<dtClolumn.Rows.Count;i++){SetCloumnWidth(doc,sheet,i,int.Parse(dtClolumn.Rows[i]["width"].ToString()));cell=sheet.Cells.Add(n+1,i+1,dtClolumn.Rows[i]["Caption"].ToString(),cellXF);}//数据cellXF.Pattern=0;objectval="";stringdtype="";for(inti=0;i<dtData.Rows.Count;i++){for(intj=0;j<dtClolumn.Rows.Count;j++){val=dtData.Rows[i][j]==DBNull.Value?"":dtData.Rows[i][j].ToString();//val=val.ToString().Trim()==""?"":val;dtype=dtData.Rows[i][j].GetType().Name;if(val.ToString()!=""){val=Convert.ToString(val);cell=sheet.Cells.Add(i+n+2,j+1,val,cellXF);}}}dtClolumn.Dispose();dtData.Dispose();doc.Save(strPath,true);}
解决方案
解决方案二:
cellRow的范围是1到65534,你赋值65536所以报错了