我有将数据集转换为 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/