excel 导出&导出checkbox到excel

设计一个ExcelBase的基类:
public class ExcelBE
  {
      private int _row = 0;
      private int _col = 0;
      private string _text = string .Empty;
      private string _startCell = string .Empty;
      private string _endCell = string .Empty;
      private string _interiorColor = string .Empty;
      private bool _isMerge = false ;
      private int _size = 0;
      private string _fontColor = string .Empty;
      private string _format = string .Empty;
 
      public ExcelBE( int row, int col, string text, string startCell, string endCell, string interiorColor, bool isMerge, int size, string fontColor, string format)
      {
          _row = row;
          _col = col;
          _text = text;
          _startCell = startCell;
          _endCell = endCell;
          _interiorColor = interiorColor;
          _isMerge = isMerge;
          _size = size;
          _fontColor = fontColor;
          _format = format;
      }
 
      public ExcelBE()
      { }
 
      public int Row
      {
          get { return _row; }
          set { _row = value; }
      }
 
      public int Col
      {
          get { return _col; }
          set { _col = value; }
      }
 
      public string Text
      {
          get { return _text; }
          set { _text = value; }
      }
 
      public string StartCell
      {
          get { return _startCell; }
          set { _startCell = value; }
      }
 
      public string EndCell
      {
          get { return _endCell; }
          set { _endCell = value; }
      }
 
      public string InteriorColor
      {
          get { return _interiorColor; }
          set { _interiorColor = value; }
      }
 
      public bool IsMerge
      {
          get { return _isMerge; }
          set { _isMerge = value; }
      }
 
      public int Size
      {
          get { return _size; }
          set { _size = value; }
      }
 
      public string FontColor
      {
          get { return _fontColor; }
          set { _fontColor = value; }
      }
 
      public string Formart
      {
          get { return _format; }
          set { _format = value; }
      }
 
  }
接下来创建ExcelBase.cs:
 
public class ExcelBase
{
     private Microsoft.Office.Interop.Excel.Application app = null ;
     private Microsoft.Office.Interop.Excel.Workbook workbook = null ;
     private Microsoft.Office.Interop.Excel.Worksheet worksheet = null ;
     private Microsoft.Office.Interop.Excel.Range workSheet_range = null ;
 
     public ExcelBase()
     {
         createDoc();
     }
 
     public void createDoc()
     {
         try
         {
             app = new Microsoft.Office.Interop.Excel.Application();
             app.Visible = true ;
             workbook = app.Workbooks.Add(1);
             worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
         }
         catch (Exception e)
         {
             Console.Write( "Error" );
         }
         finally
         {
         }
     }
 
     public void InsertData(ExcelBE be)
     {
         worksheet.Cells[be.Row, be.Col] = be.Text;
         workSheet_range = worksheet.get_Range(be.StartCell, be.EndCell);
         workSheet_range.MergeCells = be.IsMerge;
         workSheet_range.Interior.Color = GetColorValue(be.InteriorColor);
         workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
         workSheet_range.ColumnWidth = be.Size;
         workSheet_range.Font.Color = string .IsNullOrEmpty(be.FontColor) ? System.Drawing.Color.White.ToArgb() : System.Drawing.Color.Black.ToArgb();
         workSheet_range.NumberFormat = be.Formart;
     }
 
     private int GetColorValue( string interiorColor)
     {
         switch (interiorColor)
         {
             case "YELLOW" :
                 return System.Drawing.Color.Yellow.ToArgb();
             case "GRAY" :
                 return System.Drawing.Color.Gray.ToArgb();
             case "GAINSBORO" :
                 return System.Drawing.Color.Gainsboro.ToArgb();
             case "Turquoise" :
                 return System.Drawing.Color.Turquoise.ToArgb();
             case "PeachPuff" :
                 return System.Drawing.Color.PeachPuff.ToArgb();
 
             default :
                 return System.Drawing.Color.White.ToArgb();
         }
     }
}
 
调用的代码如下:
private void btnRun_Click( object sender, EventArgs e)
{
     ExcelBase excel = new ExcelBase();
     //creates the main header
     ExcelBE be = null ;
     be = new ExcelBE (5, 2, "Total of Products" , "B5" , "D5" , "YELLOW" , true , 10, "n" , null );
     excel.InsertData(be);
     //creates subheaders
     be = new ExcelBE (6, 2, "Sold Product" , "B6" , "B6" , "GRAY" , true , 10, "" , null );
     excel.InsertData(be);
     be= new ExcelBE(6, 3, "" , "C6" , "C6" , "GRAY" , true , 10, "" , null );
     excel.InsertData(be);
     be= new ExcelBE (6, 4, "Initial Total" , "D6" , "D6" , "GRAY" , true , 10, "" , null );
     excel.InsertData(be);
     //add Data to cells
     be= new ExcelBE (7, 2, "114287" , "B7" , "B7" , null , false ,10, "" , "#,##0" );
     excel.InsertData(be);
     be= new ExcelBE (7, 3, "" , "C7" , "C7" , null , false ,10, "" , null );
     excel.InsertData(be);
     be = new ExcelBE(7, 4, "129121" , "D7" , "D7" , null , false , 10, "" , "#,##0" );
     excel.InsertData(be);
     //add percentage row
     be = new ExcelBE(8, 2, "" , "B8" , "B8" , null , false , 10, "" , "" );
     excel.InsertData(be);
     be = new ExcelBE(8, 3, "=B7/D7" , "C8" , "C8" , null , false , 10, "" , "0.0%" );
     excel.InsertData(be);
     be = new ExcelBE(8, 4, "" , "D8" , "D8" , null , false , 10, "" , "" );
     excel.InsertData(be);
     //add empty divider
     be = new ExcelBE(9, 2, "" , "B9" , "D9" , "GAINSBORO" , true , 10, "" , null );
     excel.InsertData(be);   
 
}
//C# 导出checkbox到excel

  public void ExportCheckbox()
    {
        // Start excel
        Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
        oXL.Visible = true;

        // Get a sheet
        Microsoft.Office.Interop.Excel._Workbook oWB = (Microsoft.Office.Interop.Excel._Workbook)oXL.Workbooks.Add(System.Reflection.Missing.Value);
        Microsoft.Office.Interop.Excel._Worksheet oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;

        // Get ole objects and add new one
        Microsoft.Office.Interop.Excel.OLEObjects objs = oSheet.OLEObjects() as Microsoft.Office.Interop.Excel.OLEObjects;

        // Here is the method that is posted in the answer
        Microsoft.Office.Interop.Excel.OLEObject obj = objs.Add("Forms.CheckBox.1",
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            false,
            false,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            65.25,
            24,
            108,
            21);
        ((Microsoft.Vbe.Interop.Forms.CheckBox)obj.Object).set_Value("true");

((Microsoft.Vbe.Interop.Forms.CheckBox)obj.Object).Caption = "Test";
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值