网上找过了,找到的都是针对office2000的,都是参数有问题,改为Type.missing编译通过,但运行没通过,一导出就有问题了,晕那位高手有,拿出来share一下吧

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web; namespace AAAAA
    {    /// <summary> 
        /// ExcelEdit 
        /// </summary> 
        public class ExcelEdit
        {        public string mFileName;
            private Microsoft.Office.Interop.Excel.Application oAP;
            private Microsoft.Office.Interop.Excel.Workbooks oWBS;
            private Microsoft.Office.Interop.Excel.Workbook oWB;        public ExcelEdit()
            {
            }
            
            public void Create()
            {
                oAP = new Microsoft.Office.Interop.Excel.Application();
                oWBS = oAP.Workbooks;
                oWB = oWBS.Add(true);
            }
           
            public void Open(string fileName)
            {
                oAP = new Microsoft.Office.Interop.Excel.Application();
                oWBS = oAP.Workbooks;
                oWB = oWBS.Open( fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                mFileName = fileName;
            }
           
            public Microsoft.Office.Interop.Excel.Worksheet GetSheet(string sheetName)
            {
                Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[sheetName];
                return workSheet;
            }
           
            public object AddSheet(string sheetName)
            {
                Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                workSheet.Name = sheetName;
                return workSheet;
            }
            
            public void DelSheet(string sheetName)
            {
                ((Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[sheetName]).Delete();
            }
            
            public object RenameSheet(string oldSheetName, string newSheetName)
            {
                Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[oldSheetName];
                workSheet.Name = newSheetName;
                return workSheet;
            }
            public object RenameSheet(Microsoft.Office.Interop.Excel.Worksheet sheet, string newSheetName)
            {
                sheet.Name = newSheetName;
                return sheet;
            }
            
            public string GetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y)
            {
                return workSheet.Cells[x,y].ToString();
                
            }
            public string GetCellValue(string workSheetName, int x, int y)
            {
                return GetSheet(workSheetName).Cells[x,y].ToString();
            }
            
            public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y, object value)
            {
                workSheet.Cells[x,y] = value;
            }
            public void SetCellValue(string workSheet, int x, int y, object value)
            {
                GetSheet(workSheet).Cells[x,y] = value;
            }
            
            public void SetCellProperty(Microsoft.Office.Interop.Excel.Worksheet workSheet, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment
            )
            {
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment;
            }
            public void SetCellProperty(string workSheetName, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment
            )
            {
                Microsoft.Office.Interop.Excel.Worksheet workSheet = GetSheet(workSheetName);
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment;
            }
           
            public void MergeCells(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x1, int y1, int x2, int y2)
            {            workSheet.get_Range(workSheet.Cells[x1, y1], workSheet.Cells[x2, y2]).Merge(Type.Missing);
            }
            public void MergeCells(string workSheetName, int x1, int y1, int x2, int y2)
            {
                GetSheet(workSheetName).get_Range(GetSheet(workSheetName).Cells[x1, y1], GetSheet(workSheetName).Cells[x2, y2]).Merge(Type.Missing);
            }
            
            public void InsertTable(System.Data.DataTable dt, string workSheetName, int startX, int startY)
            {
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    for (int j = 0; j <= dt.Columns.Count - 1; j++)
                    {
                        GetSheet(workSheetName).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
                    }
                }
            }
            public void InsertTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet workSheet, int startX, int startY)
            {
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    for (int j = 0; j <= dt.Columns.Count - 1; j++)
                    {
                        workSheet.Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
                    }
                }
            }
            
            public bool Save()
            {
                if (mFileName == string.Empty)
                {
                    return false;
                }
                else
                {
                    try
                    {
                        oWB.Save();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(ex.Message);
                    }
                }
            }
            
            public bool SaveAs(object fileName)
            {
                try
                {
                    oWB.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing);
                    return true;
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(ex.Message);
                }
            }
           
            public void Close()
            {
                oWB.Close(true, mFileName, Type.Missing);
                oWBS.Close();
                oAP.Quit();
                oWB = null;
                oWBS = null;
                oAP = null;
                GC.Collect();
            }
        }

      

  2.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web; namespace AAAAA
    {    /// <summary> 
        /// ExcelEdit 
        /// </summary> 
        public class ExcelEdit
        {        public string mFileName;
            private Microsoft.Office.Interop.Excel.Application oAP;
            private Microsoft.Office.Interop.Excel.Workbooks oWBS;
            private Microsoft.Office.Interop.Excel.Workbook oWB;        public ExcelEdit()
            {
            }
            
            public void Create()
            {
                oAP = new Microsoft.Office.Interop.Excel.Application();
                oWBS = oAP.Workbooks;
                oWB = oWBS.Add(true);
            }
           
            public void Open(string fileName)
            {
                oAP = new Microsoft.Office.Interop.Excel.Application();
                oWBS = oAP.Workbooks;
                oWB = oWBS.Open( fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                mFileName = fileName;
            }
           
            public Microsoft.Office.Interop.Excel.Worksheet GetSheet(string sheetName)
            {
                Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[sheetName];
                return workSheet;
            }
           
            public object AddSheet(string sheetName)
            {
                Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                workSheet.Name = sheetName;
                return workSheet;
            }
            
            public void DelSheet(string sheetName)
            {
                ((Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[sheetName]).Delete();
            }
            
            public object RenameSheet(string oldSheetName, string newSheetName)
            {
                Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[oldSheetName];
                workSheet.Name = newSheetName;
                return workSheet;
            }
            public object RenameSheet(Microsoft.Office.Interop.Excel.Worksheet sheet, string newSheetName)
            {
                sheet.Name = newSheetName;
                return sheet;
            }
            
            public string GetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y)
            {
                return workSheet.Cells[x,y].ToString();
                
            }
            public string GetCellValue(string workSheetName, int x, int y)
            {
                return GetSheet(workSheetName).Cells[x,y].ToString();
            }
            
            public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x, int y, object value)
            {
                workSheet.Cells[x,y] = value;
            }
            public void SetCellValue(string workSheet, int x, int y, object value)
            {
                GetSheet(workSheet).Cells[x,y] = value;
            }
            
            public void SetCellProperty(Microsoft.Office.Interop.Excel.Worksheet workSheet, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment
            )
            {
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment;
            }
            public void SetCellProperty(string workSheetName, int Startx, int Starty, int Endx, int Endy, string name, int size, int color, bool bold, object hAlignment
            )
            {
                Microsoft.Office.Interop.Excel.Worksheet workSheet = GetSheet(workSheetName);
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Name = name;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Bold = bold;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Size = size;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).Font.Color = color;
                workSheet.get_Range(workSheet.Cells[Startx, Starty], workSheet.Cells[Endx, Endy]).HorizontalAlignment = hAlignment;
            }
           
            public void MergeCells(Microsoft.Office.Interop.Excel.Worksheet workSheet, int x1, int y1, int x2, int y2)
            {            workSheet.get_Range(workSheet.Cells[x1, y1], workSheet.Cells[x2, y2]).Merge(Type.Missing);
            }
            public void MergeCells(string workSheetName, int x1, int y1, int x2, int y2)
            {
                GetSheet(workSheetName).get_Range(GetSheet(workSheetName).Cells[x1, y1], GetSheet(workSheetName).Cells[x2, y2]).Merge(Type.Missing);
            }
            
            public void InsertTable(System.Data.DataTable dt, string workSheetName, int startX, int startY)
            {
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    for (int j = 0; j <= dt.Columns.Count - 1; j++)
                    {
                        GetSheet(workSheetName).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
                    }
                }
            }
            public void InsertTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet workSheet, int startX, int startY)
            {
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    for (int j = 0; j <= dt.Columns.Count - 1; j++)
                    {
                        workSheet.Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
                    }
                }
            }
            
            public bool Save()
            {
                if (mFileName == string.Empty)
                {
                    return false;
                }
                else
                {
                    try
                    {
                        oWB.Save();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(ex.Message);
                    }
                }
            }
            
            public bool SaveAs(object fileName)
            {
                try
                {
                    oWB.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing);
                    return true;
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(ex.Message);
                }
            }
           
            public void Close()
            {
                oWB.Close(true, mFileName, Type.Missing);
                oWBS.Close();
                oAP.Quit();
                oWB = null;
                oWBS = null;
                oAP = null;
                GC.Collect();
            }
        }

      

  3.   

    http://www.cnblogs.com/peterzb/archive/2009/07/06/1517395.html
      

  4.   

    用文件流導出吧! 
    去CSDN下載區 找 C# EXCEL 流 
    有實例,它不受 EXCEL 版本影響!
      

  5.   

    可以使用第三方控件NickLee,速度比这样快多了