你好,我有这个代码,我创建了一个xlsx文件,我需要预先设置xlsx表格单元格的宽度.

using (ExcelPackage p = new ExcelPackage())
            {
                String filepath = "C://StatsYellowPages.csv";
                DataSet ds = ExportCSVFileToDataset(filepath, "tblCustomers", "\t");
                //Here setting some document properties              
                p.Workbook.Properties.Title = "StatsYellowPages";

                //Create a sheet
                p.Workbook.Worksheets.Add("Sample WorkSheet");
                ExcelWorksheet ws = p.Workbook.Worksheets[1];
                ws.Name = "StatsYellowPages"; //Setting Sheet's name

                //Merging cells and create a center heading for out table
                ws.Cells[1, 1].Value = "StatsYellowPages";
                ws.Cells[1, 1, 1, ds.Tables[0].Columns.Count].Merge = true;
                ws.Cells[1, 1, 1, ds.Tables[0].Columns.Count].Style.Font.Bold = true;
                ws.Cells[1, 1, 1, ds.Tables[0].Columns.Count].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                int colIndex = 1;
                int rowIndex = 2;

                foreach (DataColumn dc in ds.Tables[0].Columns) //Creating Headings
                {
                    var cell = ws.Cells[rowIndex, colIndex];

                    //Setting the background color of header cells to Gray
                    var fill = cell.Style.Fill;
                    fill.PatternType = ExcelFillStyle.Solid;
                    fill.BackgroundColor.SetColor(Color.Gray);


                    //Setting Top/left,right/bottom borders.
                    var border = cell.Style.Border;
                    border.Bottom.Style = ExcelBorderStyle.Thin;
                    border.Top.Style = ExcelBorderStyle.Thin;
                    border.Left.Style = ExcelBorderStyle.Thin;
                    border.Right.Style = ExcelBorderStyle.Thin;

                    //Setting Heading Value in cell
                    cell.Value = dc.ColumnName;

                    colIndex++;
                }

                foreach (DataRow dr in ds.Tables[0].Rows) // Adding Data into rows
                {
                    colIndex = 1;
                    rowIndex++;
                    foreach (DataColumn dc in ds.Tables[0].Columns)
                    {
                        var cell = ws.Cells[rowIndex, colIndex];
                        //Setting Value in cell
                        cell.Value = dr[dc.ColumnName].ToString();
                        //Setting borders of cell
                        var border = cell.Style.Border;                      
                        colIndex++;
                    }
                }


                //Generate A File with Random name
                Byte[] bin = p.GetAsByteArray();
                string file = "c:\\StatsYellowPages.xlsx";
                File.WriteAllBytes(file, bin);

推荐答案

我发现在我填写了表格上的所有数据后,设置列宽很有效:

ws.Column(1).Width = 50;

还有autoFitColumns方法,但它忽略了带有公式和换行文本的单元格,因此对我不起作用.

ws.Cells["A1:K20"].AutoFitColumns();

.net相关问答推荐

为什么.Net 8.0.100是预览版?

ECS服务无法从Cognito获取配置

如何规范机器之间连字符的排序顺序?

为什么具有可为空值的 struct 的 HashSet 非常慢?

在 C# 中输入按键

在一个 LINQ 查询中获取两列的总和

将客户端证书添加到 .NET Core HttpClient

有没有办法以编程方式最小化窗口

我应该默认推荐密封类吗?

实例化具有运行时确定类型的对象

File.ReadAllLines() 和 File.ReadAllText() 有什么区别?

将 Topshelf 应用程序安装为 Windows 服务

图像与位图类

修剪数组中的所有字符串

双倍的? = 双倍? + 双倍?

DataGridView 在我的两个屏幕之一上的可怕重绘性能

在不使用while循环的情况下找到最里面的异常?

System.IO.IOException:使用 System.IO.Path.GetTempFileName() 时文件存在 - 解决方案?

如何为 Dapper 查询动态创建参数

如何从 webclient 获取状态码?