zoukankan      html  css  js  c++  java
  • 使用Aspose.Cells组件生成Excel文件列宽度自适应时出错:Column width must be between 0 and 255

    错误提示:Column width must be between 0 and 255

    出错处

             /// <summary>          
            /// 设置表页的列宽度自适应          
            /// </summary>          
            /// <param name="sheet">worksheet对象</param>          
            void setColumnWithAuto(Worksheet sheet)          
            {              
                Cells cells = sheet.Cells;              
                int columnCount = cells.MaxColumn;  //获取表页的最大列数              
                int rowCount = cells.MaxRow;        //获取表页的最大行数                            
                for (int col = 0; col < columnCount; col++)              
                {                  
                    sheet.AutoFitColumn(col, 0, rowCount);              
                }              
                for (int col = 0; col < columnCount; col++)              
                {  
                    cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30);
                }          
            }

    原因分析:撰写代码者是参考网上的代码,如http://www.cnblogs.com/kdkler/p/4508134.html,但由于输出时单个单元格内内容过长,导致报错。

    修改:增加一层判断

       /// <summary>          
            /// 设置表页的列宽度自适应          
            /// </summary>         
            /// /// <param name="sheet">worksheet对象</param>       
            public static void setColumnWithAuto(Worksheet sheet)
            {
                Cells cells = sheet.Cells;
                int columnCount = cells.MaxColumn + 1;
                //获取表页的最大列数            
                int rowCount = cells.MaxRow;
                //获取表页的最大行数                     
                for (int col = 0; col < columnCount; col++)
                {
                    sheet.AutoFitColumn(col, 0, rowCount);
                }
                for (int col = 0; col < columnCount; col++)
                {
                    int pixel = cells.GetColumnWidthPixel(col)+30;
                    if (pixel > 255)
                    {
                        cells.SetColumnWidthPixel(col, 255);
                    }
                    else
                    {
                        cells.SetColumnWidthPixel(col, pixel);
                    }
                }
            }

  • 相关阅读:
    LOJ-10096(强连通+bfs)
    LOJ-10095(缩点的特殊使用)
    LOJ-10094(强连通分量)
    LOJ-10092(最大半连通子图)
    【BZOJ3489】A simple rmq problem(KD-Tree)
    UVA10384 推门游戏 The Wall Pushers(IDA*)
    [SCOI2005]骑士精神(IDA*)
    浅谈A*算法
    【模板】K-D Tree
    【XSY1953】【BZOJ4012】【HNOI2015】开店(动态点分治)
  • 原文地址:https://www.cnblogs.com/mcwym/p/6888166.html
Copyright © 2011-2022 走看看