问题描述
asp.net项目里面使用usingMicrosoft.Office.Interop.Excel组件做报表导出的使用报给定关键字不在字典中~~~~本地测试没有问题,上传到服务器上后报这个异常注:2008版本服务器麻烦各位帮忙看看,小弟先谢谢了///<summary>///DataSet导出Excel///</summary>///<paramname="page">页面对象</param>///<paramname="dv">用于导出的数据</param>///<paramname="tmpExpDir">文件路径</param>///<paramname="refFilenameStart">文件名前缀</param>///<paramname="refFilenameEnd">文件名后缀</param>///<paramname="sheetName">Sheet名称</param>///<paramname="sheetSize">sheet包含的数据行数,不包括标题行</param>///<paramname="setBorderLint">导出完成后,是否给数据加上边框线</param>///<paramname="sheetTableName">表名</param>///<paramname="CellName">表列名</param>///<paramname="TableDescription">表数据描述</param>///<paramname="totallist">合计业务</param>///<paramname="isClosePage">是否关闭页面</param>///<returns></returns>publicboolWebExportToExcel(System.Web.UI.Pagepage,DataSetdv,StringtmpExpDir,StringrefFilenameStart,StringrefFilenameEnd,String[]sheetName,intsheetSize,BooleansetBorderLint,String[]sheetTableName,Dictionary<string,string>CellName,String[]TableDescription,List<TotalBussiness>totallist,boolisClosePage){Int32RowsToDivideSheet=sheetSize;//计算sheet行数Int32sheetCount=dv.Tables.Count;GC.Collect();//回收其他垃圾Applicationexcel;_WorkbookxBk;_WorksheetxSt=null;objectobjOpt=System.Reflection.Missing.Value;excel=newApplication();xBk=excel.Workbooks.Add(true);try{//申请循环中要使用的变量Int32dvRowStart=0;Int32dvRowEnd;Int32rowIndex=0;Int32colIndex=0;//对全部Sheet进行操作for(intsheetIndex=0;sheetIndex<sheetCount;sheetIndex++){//设置初始化行和列rowIndex=1;colIndex=1;//计算起始行dvRowStart=1;//sheetIndex*RowsToDivideSheet//计算结束行dvRowEnd=RowsToDivideSheet;//dvRowStart+RowsToDivideSheet-1if(dvRowEnd>dv.Tables[sheetIndex].Rows.Count){dvRowEnd=dv.Tables[sheetIndex].Rows.Count+1;}//创建一个sheetif(null==xSt){xSt=(_Worksheet)xBk.Worksheets.Add(Type.Missing,Type.Missing,1,Type.Missing);}else{xSt=(_Worksheet)xBk.Worksheets.Add(Type.Missing,xSt,1,Type.Missing);}//设置SheetNamexSt.Name=sheetName[sheetIndex].ToString();#region//表头//设置表头Microsoft.Office.Interop.Excel.Rangerange=xSt.Range[excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,dv.Tables[sheetIndex].Columns.Count]];range.Merge(0);//设置表头格式//xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment=XlVAlign.xlVAlignCenter;//设置标题居中对齐range.HorizontalAlignment=XlVAlign.xlVAlignCenter;//设置单元格为文本range.NumberFormatLocal="@";//xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).Font.Bold=true;range.Font.Size=15;//excel.Cells[rowIndex,colIndex++]=col.ColumnName;range.Font.Name="黑体";range.Borders.LineStyle=1;excel.Cells[1,1]=sheetTableName[sheetIndex];range=null;#endregion#region//表描述rowIndex++;range=xSt.Range[excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,dv.Tables[sheetIndex].Columns.Count]];//设置单元格为文本range.NumberFormatLocal="@";range.Font.Size=12;excel.Cells[rowIndex,dv.Tables[sheetIndex].Columns.Count]=TableDescription[sheetIndex];range=null;rowIndex++;#endregion//取得标题for(intcol=0;col<dv.Tables[sheetIndex].Columns.Count;col++){Microsoft.Office.Interop.Excel.Ranger=xSt.Range[excel.Cells[rowIndex,col+1],excel.Cells[rowIndex,col+1]];//设置标题格式//xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment=XlVAlign.xlVAlignCenter;r.HorizontalAlignment=XlVAlign.xlVAlignCenter;//设置单元格为文本r.NumberFormatLocal="@";r.Font.Name="黑体";//设置标题居中对齐//xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).Font.Bold=true;r.Font.Bold=true;r.ColumnWidth=17;//填值,并进行下一列//excel.Cells[rowIndex,colIndex++]=col.ColumnName;Stringaa=CellName[(sheetIndex.ToString()+"|"+(col).ToString())];excel.Cells[rowIndex,col+1]=aa;}TotalBussinesstotal=null;total=totallist.Find(b=>b.Index==sheetIndex);//取得DataSet中的数据foreach(DataRowdrindv.Tables[sheetIndex].Rows){//新起一行,当前单元格移至行首rowIndex++;colIndex=1;for(inti=0;i<dv.Tables[sheetIndex].Columns.Count;i++){Stringaa=dr[i].ToString();if(total!=null&&total.IsTotal&&total.ListIndex!=null&&total.ListIndex.Count>0){if(total.ListIndex.ContainsKey(sheetIndex.ToString()+"|"+(i).ToString())){Decimaltotalnum=Winkk.HelpUtility.ParseHelper.DecimalParse(total.ListIndex[sheetIndex.ToString()+"|"+(i).ToString()],0);totalnum+=Winkk.HelpUtility.ParseHelper.DecimalParse(dr[i],0);total.ListIndex.Remove(sheetIndex.ToString()+"|"+(i).ToString());total.ListIndex[sheetIndex.ToString()+"|"+(i).ToString()]=totalnum.ToString();}}range=xSt.Range[excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]];range.NumberFormatLocal="@";range.Font.Size=12;range.Borders.LineStyle=1;excel.Cells[rowIndex,colIndex]=""+aa+"";colIndex++;}}for(intsty=0;sty<2;sty++){rowIndex++;//取得空格格式foreach(DataColumncolindv.Tables[sheetIndex].Columns){Int32colindex=1;Microsoft.Office.Interop.Excel.Ranger=xSt.Range[excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]];//设置标题格式//xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment=XlVAlign.xlVAlignCenter;r.HorizontalAlignment=XlVAlign.xlVAlignCenter;//设置单元格为文本r.NumberFormatLocal="@";range.Borders.LineStyle=1;range.Font.Name="黑体";range.Font.Size=12;//设置标题居中对齐//xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).Font.Bold=true;//填值,并进行下一列//excel.Cells[rowIndex,colIndex++]=col.ColumnName;excel.Cells[rowIndex,colindex]="";colindex++;}}
解决方案
解决方案二:
代码没发完...#region//合计if(total!=null&&total.IsTotal&&total.ListIndex!=null&&total.ListIndex.Count>0){rowIndex++;range=xSt.Range[excel.Cells[rowIndex,1],excel.Cells[rowIndex,1]];range.HorizontalAlignment=XlVAlign.xlVAlignCenter;range.NumberFormatLocal="@";range.Font.Name="黑体";range.Font.Size=12;range.Borders.LineStyle=1;excel.Cells[rowIndex,1]="合计:";for(intcol=1;col<dv.Tables[sheetIndex].Columns.Count;col++){range=xSt.Range[excel.Cells[rowIndex,col+1],excel.Cells[rowIndex,col+1]];range.HorizontalAlignment=XlVAlign.xlVAlignDistributed;range.NumberFormatLocal="@";range.Font.Name="黑体";range.Font.Size=12;range.Borders.LineStyle=1;if(total.ListIndex.ContainsKey(sheetIndex.ToString()+"|"+col.ToString())){excel.Cells[rowIndex,col+1]=Decimal.Parse(total.ListIndex[sheetIndex.ToString()+"|"+col.ToString()]).ToString("#.##");}else{excel.Cells[rowIndex,col+1]="";}}}#endregionRangeallDataWithTitleRange=xSt.Range[excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]];allDataWithTitleRange.Select();allDataWithTitleRange.Columns.AutoFit();if(setBorderLint){allDataWithTitleRange.Borders.LineStyle=1;}}StringrefFilename=Guid.NewGuid().ToString()+".xls";//Sheet循环结束StringabsFileName=HttpContext.Current.Server.MapPath(System.IO.Path.Combine(tmpExpDir,refFilename));if(System.IO.File.Exists(absFileName)){System.IO.File.Delete(absFileName);}xBk.SaveAs(absFileName,Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7,null,null,false,false,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);xBk.Close(false,objOpt,objOpt);excel.Quit();System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);xBk=null;excel=null;xSt=null;GC.Collect();stringdownloadFileName=refFilenameStart+refFilenameEnd+".xls";//page.Response.Write("<metahttp-equiv=Content-Typecontent="text/html;charset=UTF-8">");page.Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-8");page.Response.AppendHeader("Content-Disposition","attachment;filename="+System.Web.HttpUtility.UrlEncode(downloadFileName,System.Text.Encoding.UTF8));page.Response.ContentType="application/vnd.ms-excel";page.Response.WriteFile(absFileName);page.Response.Flush();//if(System.IO.File.Exists(absFileName))//System.IO.File.Delete(absFileName);}catch(Exceptionex){if(excel!=null){try{foreach(Microsoft.Office.Interop.Excel.Workbookwbinexcel.Workbooks){if(wb!=null){try{wb.Saved=true;}catch(Exceptione){LogHelper.WriteLog(LogType.SysException,Winkk.LogUtility.LogHelper.CreateSysErrorLogInstance(SysErrorLogType.SystemEL,9999,ex.StackTrace,e));throwe;}}}excel.Workbooks.Close();excel.Quit();excel=null;GC.Collect();}catch(Exceptionesg){LogHelper.WriteLog(LogType.SysException,Winkk.LogUtility.LogHelper.CreateSysErrorLogInstance(SysErrorLogType.SystemEL,9999,esg.StackTrace,esg));throwesg;}}LogHelper.WriteLog(LogType.SysException,Winkk.LogUtility.LogHelper.CreateSysErrorLogInstance(SysErrorLogType.SystemEL,9999,ex.StackTrace,ex));throwex;}finally{if(isClosePage)page.Response.End();}returntrue;}
解决方案三:
同遇到给定关键字不在字典中http://topic.csdn.net/u/20120208/09/4eeb9d3e-5c7e-4d3e-89d8-77bd45ffbb3c.html?46901
解决方案四:
求高人解答