zoukankan      html  css  js  c++  java
  • c# 操作excle

    添加引用  Microsoft.Office.Interop.Excel;

    添加命名空间 using Excel = Microsoft.Office.Interop.Excel;

    //创建接口变量------------------------------------------

    _Workbook _xlWorkBook = null;
    Worksheet _xlWorkSheet = null;
    Excel.Application _xlApp = null;
    

    //创建excle Application----------------------------------

    _xlApp= new Excel.Application();
    //_xlApp.DisplayAlerts = false; //设置报警窗口
    _xlApp.Visible = true;             //设置显示
     // _xlApp.ScreenUpdating = false; //设置屏幕刷新
    

    //创建workbook ----------------------------------------

    //打开已存在的workbook  path是文件路径
    _xlWorkBook = _xlApp.Workbooks.Open(path, System.Type.Missing, System.Type.Missing, System.Type.Missing,System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
    

    //创建worksheet--------------------------------------------

    //得到指定的sheet
    //Excel.Worksheet xlsWorkSheet = (Worksheet)xlsWorkBook.Worksheets["2013年"];
    _xlWorkSheet = (Worksheet)_xlWorkBook.Sheets["2013年"];//得到指定的sheet
    //_xlWorkSheet =(Worksheet) _xlWorkBook.ActiveSheet;//得到当前活跃sheet
    

    //获取所有sheet

    Sheets xlsWorkSheets = _xlWorkBook.Worksheets; 
    foreach (var s in xlsWorkSheets)
    {
     Worksheet xlsWorkSheet = s as Worksheet;
     string ss= xlsWorkSheet.Name;
    }
    

    //增加sheet页

    // Worksheet workSheet = (Worksheet)_xlWorkBook.Worksheets.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
    

     //创建Excel.Range----------------------------------------------------- 

                 //获取单元格内容
                 var cell= (Range)_xlWorkSheet.Cells[1, 3];
                 var cellvalue = cell.Value.ToString();
                 string pathd = @"c:wellname.txt";
                 _xlWorkSheet.Hyperlinks.Add(cell, pathd); //添加超链接
    
                 //RowHeight   "1:1"表示第一行, "1:2"表示,第一行和第二行 
                  ((Excel.Range)_xlWorkSheet.Rows["1:1", System.Type.Missing]).RowHeight = 100;
                 //ColumnWidth "A:B"表示第一列和第二列, "A:A"表示第一列
                  ((Excel.Range)_xlWorkSheet.Columns["A:B", System.Type.Missing]).ColumnWidth = 10;
                  Excel.Range excelRange = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[10, 5], _xlWorkSheet.Cells[10, 5]);
                  excelRange.Select(); //选中区域
                  _xlApp.ActiveWindow.FreezePanes = true;//冻结字段
                  excelRange.Borders.LineStyle = 1; //区域边框线型
                  excelRange.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous;//区域顶部边框虚线
                  excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlMedium; //单元格下边框线粗细
                  excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).ColorIndex = 3;//边框色彩
                  excelRange.Font.Size = 15;//字体大小
                  excelRange.Font.Underline = true;//下划线
                  excelRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;//字体在单元格内的对其方式
                  excelRange.ColumnWidth = 15;//单元格的宽度 
                  excelRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb();//单元格的背景色
                  //合并单元格
                  excelRange.Merge(excelRange.MergeCells);
                  _xlWorkSheet.get_Range("A15", "B15").Merge(_xlWorkSheet.get_Range("A15", "B15").MergeCells); 
    

      

      

  • 相关阅读:
    System.AccessViolationException”类型的第一次机会异常在 System.Data.dll 中发生 其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
    解决浏览器显示长数字不能自动换行或长字母不能换行的问题
    索引 -1 没有值
    vrpie在Visio Studio 中无法调试的问题
    vrpie下实现vrp模型和javascript的交互
    IE下Date.parse出现NaN有关问题解决
    SQL Server 一些关键字详解(一)
    SQL Server 一些关键字详解(二)
    WPF WebBrowser 遇到的问题
    日历代码
  • 原文地址:https://www.cnblogs.com/crhdyl/p/4959029.html
Copyright © 2011-2022 走看看