请问如何用C#设置Excel的列宽?

解决方案 »

  1.   

    //列宽设置
    excelRange.ColumnWidth = 宽度;
      

  2.   

    Don't Use VBA to Accesss Excel. There are many many problem.
    First is Different Excel Version in customer computer
    Second You can't control Excel Application Process.
    Last Use VBA Technical is Very slow.
    So I suggest you use F1Book activeX,I have a free F1Book ActiveX.
    The Source code as flow:
    /// <summary>
    /// 导出到Excel中
    /// </summary>
    private void ExportToExcel(string fileName)
    {
    DataTable dicDt = GetDictionary();
    if(dicDt != null)
    {
    try
    {
    TTF160.F1BookClass sheet = new TTF160.F1BookClass(); sheet.MinCol = 1;
    sheet.MaxCol = 6;
    sheet.MinRow = 1;
    sheet.MaxRow = dicDt.Rows.Count + 1;

    #region 设置列宽度,以及行宽度 sheet.ColWidthUnits = TTF160.F1ColWidthUnitsConstants.F1ColWidthUnitsCharacters;
    sheet.set_ColWidth(1, 256 * 25 );
    sheet.set_ColWidth(2, 256 * 25 );
    sheet.set_ColWidth(3, 256 * 15 );
    sheet.set_ColWidth(4, 256 * 7 );
    sheet.set_ColWidth(5, 256 * 8 );
    sheet.set_ColWidth(6, 256 * 4 );

    #endregion

    #region 设置边界、字体
    sheet.SetSelection(1,1,sheet.MaxRow,sheet.MaxCol);
    TTF160.F1CellFormat cellFormat = sheet.GetCellFormat();
    cellFormat.FontName = "宋体";
    cellFormat.FontSize = 10;
    cellFormat.WordWrap = true;
    cellFormat.AlignVertical = TTF160.F1VAlignConstants.F1VAlignCenter;
    cellFormat.set_BorderStyle(TTF160.F1BorderConstants.F1LeftBorder,TTF160.F1BorderStyleConstants.F1BorderThin);
    cellFormat.set_BorderStyle(TTF160.F1BorderConstants.F1RightBorder,TTF160.F1BorderStyleConstants.F1BorderThin);
    cellFormat.set_BorderStyle(TTF160.F1BorderConstants.F1TopBorder,TTF160.F1BorderStyleConstants.F1BorderThin);
    cellFormat.set_BorderStyle(TTF160.F1BorderConstants.F1BottomBorder,TTF160.F1BorderStyleConstants.F1BorderThin);
    cellFormat.set_BorderStyle(TTF160.F1BorderConstants.F1HInsideBorder,TTF160.F1BorderStyleConstants.F1BorderThin);
    cellFormat.set_BorderStyle(TTF160.F1BorderConstants.F1VInsideBorder,TTF160.F1BorderStyleConstants.F1BorderThin);
    sheet.SetCellFormat(cellFormat);
    #endregion #region 写数据
    #region 写标题 sheet.SetSelection(1,1,1,6);
    cellFormat = sheet.GetCellFormat();
    cellFormat.MergeCells = true;
    cellFormat.AlignHorizontal = TTF160.F1HAlignConstants.F1HAlignCenter;
    cellFormat.FontBold = true;
    cellFormat.FontSize = 24;
    sheet.SetCellFormat(cellFormat);
    sheet.set_TextRC(1,1,"数据字典");
    if(dicDt.Rows.Count == 0)
    {
    //没有数据时提示
    sheet.SetSelection(2,1,2,6);
    cellFormat = sheet.GetCellFormat();
    cellFormat.MergeCells = true;
    cellFormat.AlignHorizontal = TTF160.F1HAlignConstants.F1HAlignCenter;
    cellFormat.FontBold = true;
    cellFormat.FontSize = 24;
    cellFormat.FontColor = 0x0000FF;//BLUE-GREEN-RED
    sheet.SetCellFormat(cellFormat);
    sheet.set_TextRC(2,1,"Maybe there are not any user tables!");
    }
    //sheet.
    #endregion #region 写字典数据 DataRow rs;
    int j = 0;
    for(int k = 0; k < dicDt.Rows.Count;k++)
    {
    j = k + 2;
    rs = dicDt.Rows[k];
    if(rs["Flag"].ToString() == "0")
    {
    #region 空白行 //合并单元格 sheet.SetSelection(j,1,j,6);
    TTF160.F1CellFormat cellFormat1 = sheet.GetCellFormat();
    cellFormat1.MergeCells = true;
    sheet.SetCellFormat(cellFormat1);
    #endregion
    }
    else if(rs["Flag"].ToString() == "1")
    {
    #region 表名及注释 //
    sheet.set_TextRC(j,1,rs["ObjName"].ToString());
    sheet.SetSelection(j,2,j,6);
    TTF160.F1CellFormat cellFormat2 = sheet.GetCellFormat();
    cellFormat2 = sheet.GetCellFormat();
    cellFormat2.MergeCells = true;
    sheet.SetCellFormat(cellFormat2);
    sheet.set_TextRC(j,2,rs["REMARK"].ToString());
    sheet.set_RowHeight(j,256 * 2);
    #endregion
    }
    else if(rs["Flag"].ToString() == "2")
    {
    #region 列定义的标题
    sheet.set_TextRC(j,1,"列名");
    sheet.set_TextRC(j,2,"注释");
    sheet.set_TextRC(j,3,"数据类型");
    sheet.set_TextRC(j,4,"缺省值");
    sheet.set_TextRC(j,5,"是否为空");
    sheet.set_TextRC(j,6,"主键");
    sheet.SetSelection(j,1,j,6);
    cellFormat = sheet.GetCellFormat();
    cellFormat.FontBold = true;
    sheet.SetCellFormat(cellFormat);
    sheet.set_RowHeight(j,256 * 2);
    #endregion
    }
    else if(rs["Flag"].ToString() == "3")
    {
    #region 列定义 sheet.set_TextRC(j,1,rs["ObjName"].ToString());
    sheet.set_TextRC(j,2,rs["REMARK"].ToString());
    sheet.set_TextRC(j,3,rs["Type"].ToString());
    sheet.set_TextRC(j,4,rs["DefaultValue"].ToString());
    sheet.set_TextRC(j,5,rs["IsAllowNull"].ToString());
    sheet.SetSelection(j,5,j,6);
    cellFormat = sheet.GetCellFormat();
    cellFormat.AlignHorizontal = TTF160.F1HAlignConstants.F1HAlignCenter;
    sheet.set_TextRC(j,6,rs["IsPrimaryKey"].ToString());
    sheet.set_RowHeight(j,(short)(256 * 1.5)); #endregion
    }
    //sheet.set_TextRC(1, k + 1,this.DataSource.Columns[k].ColumnName);
    }
    #endregion
    #endregion
    //sheet.SetRowHeightAuto(1,1,sheet.MaxRow,sheet.MaxCol,true);
    sheet.Write(fileName,(short)TTF160.F1FileTypeConstants.F1FileExcel97);

    }
    catch(Exception E)
    {
    ExceptionDialog.Show("导出到Excel工作簿文件时出错!","错误",E);
    }
    } }
      

  3.   

    还是自己解决
    ((Excel.Range)ex.Columns["C", Type.Missing]).ColumnWidth = 4;  // 将第三列列宽设置成4