zoukankan      html  css  js  c++  java
  • c# asp net 设置 excel 列宽

    Here's a quick tip to get started working with Excel interop in C#.

    // Create a new instance of the Excel application
    excelFile = new Excel.ApplicationClass();
    Excel.Workbook workbook = excelFile.Workbooks.Add(Type.Missing); // Now create a brand new workbook
    excelFile.Visible = true; // ensure that the excel app is visible.
    Worksheet ws = (Worksheet)excelFile.ActiveSheet; // Get the current active worksheet.
    Microsoft.Office.Interop.Excel.Worksheet worksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(2); //Get more work sheet if neccessary

    ws.Activate();

    //Here is how to set the column Width

    public void SetColumnWidth(Worksheet ws, int col, int width)
    {
    ((Range)ws.Cells[1, col]).EntireColumn.ColumnWidth = width;
    }

    // Apply the setting so that it would autofit to contents
    public void AutoFitColumn(Worksheet ws, int col)
    {
    ((Range)ws.Cells[1, col]).EntireColumn.AutoFit();
    }


    How to Set Row to Bold in Excel C# Interop:

    ((Range)ws.Cells[row, 1]).EntireRow.Font.Bold = true;


    How to Set a value inside a cell in Excel:

    (Range)ws.Cells[row, column]).Value2 = item; // set the cell value at a row and column


    How to Format the Entire Column:

    ((Range)ws.Cells[1, col]).EntireColumn.NumberFormat = format;

  • 相关阅读:
    《C++ Primer》读书笔记之第15章:面向对象编程
    Color Space: HSI
    Color Space: Lab
    C# 特殊关键字
    WPF 打开文件、文件夹
    EmguCV+Win7+Visual C# 2012 配置
    C# 常用结构
    C#抽象类、抽象方法、虚方法
    C# Image 、 byte[] 、Bitmap之间的转化
    java反射机制基础
  • 原文地址:https://www.cnblogs.com/hishanghai/p/2613306.html
Copyright © 2011-2022 走看看