C#技术百科
问问你的心你有没有信心 做事情要专一坚定,执着

 public static void ExportExcel(DataSet ds, string strExcelFileName)
        {
            object objOpt = System.Reflection.Missing.Value;

            Excel.Application excel = new Excel.Application();
            excel.Visible = true;
            Excel._Workbook wkb = excel.Workbooks.Add(objOpt);
            Excel._Worksheet wks = (Excel._Worksheet)wkb.ActiveSheet;

            wks.Visible = Excel.XlSheetVisibility.xlSheetVisible;

            int rowIndex = 1;
            int colIndex = 0;

            System.Data.DataTable table = ds.Tables[0];
            foreach (DataColumn col in table.Columns)
            {
                colIndex++;
                excel.Cells[1, colIndex] = col.ColumnName;
            }

            foreach (DataRow row in table.Rows)
            {
                rowIndex++;
                colIndex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    colIndex++;
                    excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                }
            }
            //excel.Sheets[0] = "sss";
            wkb.SaveAs(strExcelFileName, objOpt, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
            wkb.Close(false, objOpt, objOpt);
            excel.Quit();
        }
      <identity   impersonate="true"   userName="administrator"   password="**********"/>
webconfig中加上

posted on 2008-07-11 10:48  王德田  阅读(310)  评论(0编辑  收藏  举报