网上看了很多,好像这样调用很好:Excel.Application();
但是需要引入什么命名空间或者dll才能运行Excel这个类呢?

解决方案 »

  1.   

    Excel.dll在Excel安装程序下,
    using Microsoft.Office.Interop.Excel;
      

  2.   

    using Microsoft.Office.Interop.Excel;  
    只有Microsoft..CSharp另外Excel.dll在已安装的office文件夹下?还是别的地方呢?
      

  3.   

    可以用ado.net操作数据库的方法的
      

  4.   

    我在引用那里找到Microsoft.Office.Tools.Excel,这个怎么用呢?
      

  5.   

    添加using Microsoft.Office.Interop.Excel;这个引用即可
    工程--添加引用--com 下面去找 只要你装了office应该就有这个了~
      

  6.   

    com 下面去有很多的microsort 的组件,至于哪个才能有using Microsoft.Office.Interop.Excel这个命名空间呢
      

  7.   

    找到了Excel,但我想打开为保存的,让用户自己选择是否已经保存,如何做呢?
      

  8.   

    給你一個類,可以實現LZ說的功能:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Web;public class ExportToExcel
    {
        public ExportToExcel()
        {    }
        #region excelHeader    private const string excelHeader =
            @"<html xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:excel""
        xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"" xmlns:html=""http://www.w3.org/TR/REC-html40""
        xmlns=""http://www.w3.org/TR/REC-html40"">
    <head>
        <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"">
        <meta name=""ProgId"" content=""Excel.Sheet"">
        <style>
    <!--table
    {mso-displayed-decimal-separator:""\."";
    mso-displayed-thousand-separator:""\,"";}
    @page
    {margin:1.0in .75in 1.0in .75in;
    mso-header-margin:.5in;
    mso-footer-margin:.5in;}
    tr
    {mso-height-source:auto;
    mso-ruby-visibility:none;}
    col
    {mso-width-source:auto;
    mso-ruby-visibility:none;}
    br
    {mso-data-placement:same-cell;}
    .style0
    {mso-number-format:General;
    text-align:general;
    vertical-align:bottom;
    white-space:nowrap;
    mso-rotate:0;
    mso-background-source:auto;
    mso-pattern:auto;
    color:windowtext;
    font-size:10.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:Arial, sans-serif;
    mso-font-charset:0;
    border:none;
    mso-protection:locked visible;
    mso-style-name:Normal;
    mso-style-id:0;}
    td
    {mso-style-parent:style0;
    padding-top:1px;
    padding-right:1px;
    padding-left:1px;
    mso-ignore:padding;
    color:windowtext;
    font-size:10.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:Arial, sans-serif;
    mso-font-charset:0;
    mso-number-format:General;
    text-align:general;
    vertical-align:bottom;
    border:none;
    mso-background-source:auto;
    mso-pattern:auto;
    mso-protection:locked visible;
    white-space:nowrap;
    mso-rotate:0;}
    .BoldColumn
    {mso-style-parent:style0;
    color:white;
    font-weight:700;
    text-align:center;
    vertical-align:middle;
    border-top:.5pt solid windowtext;
    border-right:.5pt solid windowtext;
    border-bottom:.5pt solid windowtext;
    border-left:none;
    background:#33CCCC;
    mso-pattern:auto none;}
    .StringLiteral
    {mso-style-parent:style0;
    mso-number-format:""\@"";}
    .Float
    {mso-style-parent:style0;
    mso-number-format:##FloatFormat##;}
    .Integer
    {mso-style-parent:style0;
    mso-number-format:0;}
    .DateLiteral
    {mso-style-parent:style0;
    mso-number-format:""yyyy-m-d h:mm:ss"";}
    .ReCellCaption
    {mso-style-parent:style0;
    color:white;
    font-weight:700;
    mso-generic-font-family:auto;
    mso-font-charset:134;
    text-align:center;
    vertical-align:top;
    border-top:.5pt solid windowtext;
    border-right:.5pt solid windowtext;
    border-bottom:.5pt solid windowtext;
    border-left:.5pt solid windowtext;
    background:#99CCFF;
    mso-pattern:auto none;
    white-space:normal;}
    .ReCell
    {mso-style-parent:style0;
    text-align:left;
    vertical-align:top;
    border-top:.5pt solid windowtext;
    border-right:.5pt solid windowtext;
    border-bottom:.5pt solid windowtext;
    border-left:.5pt solid windowtext;
    background:#FFFFE1;
    mso-pattern:auto none;
    white-space:normal;}
    ruby
    {ruby-align:left;}
    rt
    {color:windowtext;
    font-size:9.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    mso-generic-font-family:auto;
    mso-font-charset:134;
    mso-char-type:none;
    display:none;}
    -->
    </style>
        <!--[if gte mso 9]><xml>
     <x:ExcelWorkbook>
      <x:ExcelWorksheets>
       <x:ExcelWorksheet>
        <x:Name>Sheet1</x:Name>
        <x:WorksheetOptions>
         <x:Selected/>
         <x:ProtectContents>False</x:ProtectContents>
         <x:ProtectObjects>False</x:ProtectObjects>
         <x:ProtectScenarios>False</x:ProtectScenarios>
        </x:WorksheetOptions>
       </x:ExcelWorksheet>
      </x:ExcelWorksheets>
      <x:WindowHeight>10005</x:WindowHeight>
      <x:WindowWidth>10005</x:WindowWidth>
      <x:WindowTopX>120</x:WindowTopX>
      <x:WindowTopY>135</x:WindowTopY>
      <x:ProtectStructure>False</x:ProtectStructure>
      <x:ProtectWindows>False</x:ProtectWindows>
     </x:ExcelWorkbook>
    </xml><![endif]-->
    </head>
    <body>";    #endregion
        private const string excelTail = "</body></html>";
        private const string sheetHeader = "<table>";
        private const string sheetTail = "</table>";
        private const string rowHeader = "<tr>";
        private const string rowTail = "</tr>";
        private const string excelCell = "<td class=\"{0}\">{1}</td>";    public void Export(DataSet source, string fileName)
        {
            string tempName = DataSetToTextWriter(source);
            TransmitFile(tempName, fileName);
        }
        private string DataSetToTextWriter(DataSet source)
        {
            string tempName = Path.GetTempFileName();
            StreamWriter txtWriter = new StreamWriter(tempName);
            txtWriter.Write(excelHeader);
            if (source.Tables.Count == 0)
            {
                return "";
            }
            txtWriter.Write(sheetHeader);
            txtWriter.Write(rowHeader);        string[] columnArray;
            string[] columnText;
            int columnCount = source.Tables[0].Columns.Count;        columnArray = new string[columnCount];
            columnText = new string[columnCount];
            for (int i = 0; i < columnCount; i++)
            {
                columnArray[i] = source.Tables[0].Columns[i].ColumnName;
            }
            string columnName;
            for (int i = 0; i < columnCount; i++)
            {
                if (string.IsNullOrEmpty(columnText[i]))
                {
                    columnName = columnArray[i];            }
                else
                {
                    columnName = HttpUtility.UrlDecode(columnText[i]);
                }
                txtWriter.Write(excelCell, "BoldColumn", columnName);
            }
            string temp;
            object rowValue;
            DataView rows = source.Tables[0].DefaultView;
            foreach (DataRowView row in rows)
            {
                txtWriter.Write(rowHeader);
                for (int i = 0; i < columnCount; i++)
                {
                    rowValue = row[columnArray[i]];
                    switch (rowValue.GetType().FullName)
                    {
                        case "System.String":
                            temp = rowValue.ToString();                        txtWriter.Write(excelCell, "StringLiteral", temp);
                            break;
                        case "System.DateTime":
                            DateTime dateTime = (DateTime)rowValue;
                            txtWriter.Write(
                                excelCell, "DateLiteral", dateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                            break;
                        case "System.Boolean":
                            txtWriter.Write(excelCell, "StringLiteral", rowValue.ToString());
                            break;
                        case "System.Int16":
                        case "System.Int32":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.Byte":
                            txtWriter.Write(excelCell, "Integer", rowValue.ToString());
                            break;
                        case "System.Int64":
                        case "System.UInt64":
                            temp = rowValue.ToString();
                            if (temp.Length > 15)
                            {
                                txtWriter.Write(excelCell, "StringLiteral", temp);
                            }
                            else
                            {
                                txtWriter.Write(excelCell, "Integer", temp);
                            }
                            break;
                        case "System.Decimal":
                            temp = rowValue.ToString();
                            if (temp.Contains("."))
                            {
                                txtWriter.Write(excelCell, "Float", temp);
                            }
                            else
                            {
                                if (temp.Length > 15)
                                {
                                    txtWriter.Write(excelCell, "StringLiteral", temp);
                                }
                                else
                                {
                                    txtWriter.Write(excelCell, "Integer", temp);
                                }
                            }
                            break;
                        case "System.Double":
                            txtWriter.Write(excelCell, "Float", rowValue.ToString());
                            break;
                        case "System.DBNull":
                            txtWriter.Write(excelCell, "StringLiteral", string.Empty);
                            break;
                        default:
                            txtWriter.Write(excelCell, "StringLiteral", rowValue.ToString());
                            break;
                    }
                }
                txtWriter.Write(rowTail);
            }
            txtWriter.Write(sheetTail);
            txtWriter.Write(excelTail);
            txtWriter.Close();
            return tempName;    }
      

  9.   


        #region TransferFile    /// <summary>
        /// 蔚Web督昢奻腔還奀恅璃換怀善諦誧傷ㄛ甜刉壺還奀恅璃﹝
        /// </summary>
        /// <param name="tempFilePath">湔婓逄督昢奻腔埭還奀恅璃﹝</param>
        /// <param name="desFileName">悵湔腔諦誧傷腔恅璃</param>
        public static void TransmitFile(string tempFilePath, string desFileName)
        {
            //迡善httpㄛ羲宎狟婥
            string encodeFileName = HttpUtility.UrlEncode(desFileName);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + encodeFileName);
            HttpContext.Current.Response.TransmitFile(tempFilePath);
            HttpContext.Current.Response.Flush();
            //雄盄最燴還奀恅璃
            DeleteTempFile(tempFilePath, false);
            HttpContext.Current.Response.End();
        }    #endregion    #region DeleteTempFile    /// <summary>
        /// 雄盄最燴還奀恅璃ㄛ忑棒脹50鏃ㄛ楷珋恅璃淏婓妏蚚寀森綴藩30鏃郭彸刉壺珨棒ㄛ
        /// 僕郭彸10棒﹝
        /// </summary>
        /// <param name="tempFilePath">The temp file path.</param>
        /// <param name="deleteDirectory">岆瘁刉壺垀婓恅璃標</param>
        public static void DeleteTempFile(string tempFilePath, bool deleteDirectory)
        {
            if (!File.Exists(tempFilePath))
            {
                return;
            }
            Thread t = new Thread(delegate()
                                  {
                                      int retry = 0;
                                      int interval = 50000; //忑棒脹渾50鏃
                                      while (retry < 12)
                                      {
                                          try
                                          {
                                              Thread.Sleep(interval);
                                              if (deleteDirectory)
                                              {
                                                  string dir = Path.GetDirectoryName(tempFilePath);
                                                  Directory.Delete(dir, true);
                                              }
                                              else
                                              {
                                                  File.Delete(tempFilePath);
                                              }
                                              break;
                                          }
                                          catch (IOException)
                                          {
                                              //刉壺囮啖綴寀蔚脹渾奀潔离峈30鏃
                                              interval = 30000;
                                              retry++;
                                          }
                                          catch (Exception)
                                          {
                                              break;
                                          }
                                      }
                                  });
            t.Start();
        }    #endregion
    }