c# - 如何设置以编程方式创建的 xlsx 文件中单元格的宽度?

标签 c# .net xlsx

我有将数据集转换为 xlsx 的 C# 代码。有没有办法设置创建的 xlsx 文件的工作表的单元格或列宽?

//Get the filename      
String filepath = args[0].ToString();
//Convert the file to dataset
DataSet ds = Convert(filepath.ToString(), "tblCustomers", "\t");

//Create the excell object
Excel.Application excel = new Excel.Application();
//Create the workbook
Excel.Workbook workBook = excel.Workbooks.Add();
//Set the active sheet
Excel.Worksheet sheet = workBook.ActiveSheet;


int i = 0;
foreach (DataRow row in ds.Tables[0].Rows)
{                              
    for (int j = 0; j < row.ItemArray.Length; j++)
    {
        sheet.Cells[i + 1, j + 1] = row[j];
    }

    i++;
}

workBook.SaveAs(@"C:\fromCsv.xlsx");
workBook.Close();

最佳答案

sheet.Columns["D:D"].ColumnWidth = 17.57;

sheet.Columns[1].ColumnWidth = 17.57;

您可以在 Excel 中记录宏,然后查看生成的代码(对象模型相同)。

关于c# - 如何设置以编程方式创建的 xlsx 文件中单元格的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9079755/

相关文章:

c# - 如何将 nopcommerce 页面标题更改为我的网站名称?

c# - 使用 BookSleeve 维护一个开放的 Redis 连接

c# - 在哪里可以找到 MSDN 库图标的含义?

.net - 我们可以构造一个 `OpCode` 的实例吗?

r - 解析 HPO obo 文件以提取外部参照

excel - Coldfusion - 内存高效的 xlsx/excel 生成

python - 如何在 python 中的 xlsxwriter merge_range 函数中添加新行?

c# - 如何解析来自 HttpClient 的 JSON 字符串?

c# - 将 SID 字符串格式转换为字节数组

c# - JPG、DOC、PDF 等文件是否也编译成程序集?