下面是我的代码:请教高手指点下 我错误的地方实在不清楚了
        private void timer1_Tick(object sender, EventArgs e)
        {
            string cconnect_string = @"Data Source=ip,1433;Network Library = DBMSSOCN;Initial Catalog=hycar_ll;user id=sa;pwd=1;Persist Security Info = true";
            SqlConnection con = new SqlConnection(cconnect_string);
            try
            {
                con.Open();
                WriteLog("电信数据库链接成功!");
            }
            catch
            {
                WriteLog("电信数据库无法链接,请检查!");
            }
            string sql = "select * from mesge";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "类型");
            con.Close();
            string date = DateTime.Now.Date.ToString("yyyy-MM-dd");
            // MessageBox.Show(date);
            string time = DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString();
            if (!Directory.Exists(@"d:\excel"))
            {
                Directory.CreateDirectory(@"d:\excel");
                WriteLog("正在创建路径:" + @"d:\excel");
            }
            if (File.Exists(@"d:\excel\" + @"\" + date.Trim() + ".xls"))
            {
                File.Delete(@"d:\excel\" + date.Trim() + ".xls");
                WriteLog(@"d:\excel\" + date.Trim() + ".xls");
            }
            string ss = @"d:\excel\" + date.Trim() + ".xls";
            //doExport(ds, ss.Trim());
            WriteLog(ss);
            /////最主要是程序到了这一步不执行了导出excel
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            WriteLog("1");
            if (excel == null)
            {
                WriteLog("你系统没有安装excel");
                timer1.Start();
            }
            else
            {
                WriteLog("系统安装excel");
            }
            try
            {
                WriteLog("尝试写入excel");
                int rowIndex = 1;
                int colIndex = 0;                excel.Application.Workbooks.Add(true);
                Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
                Microsoft.Office.Interop.Excel.Range range;
                excel.Visible = false;
                //range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]);
                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++;
                        range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowIndex, colIndex]);
                        range.NumberFormat = "@";
                        excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                    }
                }
                excel.Visible = false;
                //excel.Sheets[0] = "消息";
                excel.DisplayAlerts = false;
                excel.ActiveWorkbook.SaveCopyAs(ss);                excel.Quit();
                excel = null;
                //MessageBox.Show("数据导出成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch (Exception ex)
            {
                WriteLog("错误信息: "+ex.Message);
                // MessageBox.Show(ex.ToString());
            }
            finally
            {
                WriteLog("结束");
                GC.Collect();//垃圾回收
                timer1.Start();
            }
        }
下面再贴一下我的日志文件内容:
开始链接数据库!
电信数据库链接成功!
正在创建路径:d:\excel
d:\excel\2012-03-27.xls
电信数据库链接成功!
d:\excel\2012-03-27.xls
电信数据库链接成功!
d:\excel\2012-03-27.xls
电信数据库链接成功!
d:\excel\2012-03-27.xls
电信数据库链接成功!
一直在执行这一步,后面为什么不执行了呢 日志也不写 ,跪求各位大神帮忙 
分不多但却是是拿不出来了

解决方案 »

  1.   

    不是报啥错 而是为什么我的程序执行到
    WriteLog(ss);
      /////最主要是程序到了这一步不执行下面导出excel的操作了 
      Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
      WriteLog("1");
      

  2.   

    求大神啊 主要的问题是 以上导出excel的代码在有界面下可以执行是没有问题的,但当我做成服务时程序直接跳过我的导出excel代码
    急求大神,小神,小鬼,大鬼,各路神仙 显现能吧 ,帮帮小弟下
      

  3.   

    把这段代码加上try catch,有可能是由于引用的com组件版本不一致,也有可能是区域语言设置不匹配造成
      

  4.   

    我想用的是excel2003  引用的com组件 为 microsoft office 12.0 0bject library
    你给点意见 你觉得这个引用是错误的吗?
    区域语言设置 怎么弄呢
      

  5.   

    microsoft office 12.0 object library是2007的,应该是可以兼容excel 2003,比如你的excel是中文版的,但是电脑的region里面设置的区域不是中国,也会出现问题,具体要try catch一下才知道,看看到底是什么原因造成的