microsoft.office.interop export to excel

本文关键字:to excel export interop office microsoft | 更新日期: 2023-09-27 18:01:17

我正在尝试使用Microsoft.Office.Interop.Excel从sql server数据库导出到excel;

但是我得到一个错误:

microsoft.office.interop.excel.application不包含"workbooks"的定义和没有扩展方法"workbooks"

我也添加了所有必需的引用。请告诉我如何解决这个问题。

p。

void ExportToExcel(string sqlquery, string filename)
        {
            Cursor.Current = Cursors.WaitCursor;
            SqlConnection cnn;
            string connectionString = null;
            string sql = null;
            string data = null;
            int i = 0;
            int j = 0;
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            connectionString = "Data Source=HULMOSSPROD;Initial Catalog=" + cbx_Settings.Text + ";User Id=mosssa;Password=Unilever123;";
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            sql = sqlquery;
            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
            DataSet ds = new DataSet();
            dscmd.Fill(ds);

            //==============================================================================================================
            string strLine;
            SqlCommand cmd = new SqlCommand(sqlquery, cnn);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            //Initialize the string that is used to build the file.
            strLine = "";
            //Enumerate the field names and the records that are used to build 
            //the file.
            for (int m = 0; m < 1; m++)
            {
                for (int k = 0; k <= dr.FieldCount - 1; k++)
                {
                    strLine = dr.GetName(k).ToString();
                    //xlWorkSheet.Cells[m + 1, k + 1] = strLine;
                }
            }
            //==============================================================================================================

            for (i = 1; i <= ds.Tables[0].Rows.Count; i++)
            {
                for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    data = ds.Tables[0].Rows[i - 1].ItemArray[j].ToString();
                    //xlWorkSheet.Cells[i + 1, j + 1] = data;
                }
            }

            xlWorkBook.SaveAs("C:''80IB_Reports''" + filename, Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();
            cnn.Close();
            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
            Cursor.Current = Cursors.Default;
            MessageBox.Show("Export Successful. You can find the file at C:''80IB_Reports");
        }

microsoft.office.interop export to excel

    StreamWriter sw = new StreamWriter(Response.OutputStream);
    try
    {
        conn.Open();
        OracleCommand cmd = new OracleCommand(strQuery);
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        DataSet ds = new DataSet();
        OracleDataAdapter adapter = new OracleDataAdapter(cmd);
        adapter.Fill(ds);
        foreach (DataColumn dc in ds.Tables[0].Columns)
        {
            sw.Write(dc.ColumnName);
            sw.Write("'t");
        }
        sw.WriteLine("");
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            int colindex = 0;
            foreach (DataColumn dc in ds.Tables[0].Columns)
            {
                colindex++;
                sw.Write(dr[colindex - 1].ToString());
                sw.Write("'t");
            }
            sw.WriteLine("");
        }
    }
    catch
    {
    }
    finally
    {
        sw.Close();
        conn.Close();
    }

创建应用程序实例

   Microsoft.Office.Interop.Excel.Application xlApp= new Microsoft.Office.Interop.Excel.Application();

像这样添加工作簿

Microsoft.Office.Interop.Excel.Workbook xlWorkBook= xlApp.Application.Workbooks.Add();

添加新的工作表到工作簿

        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet= xlWorkBook.Sheets.Add();