您好,感谢您的查看和帮助!
我在使用Aspose.Cells遇到了问题,DataTable有表头的,但是生成的excel没有表头。
我像您提供部分关于Aspose.Cells的代码!
// 实例化表示 excel 文件的工作簿对象。
Workbook wb = new Workbook();
// 当您创建一个新的工作簿时,默认的“ sheet1”将添加到工作簿中
Worksheet sheet = wb.Worksheets[0];
sheet.Name = "文件级";
//自定义第一行样式
Style style1 = wb.Styles[wb.Styles.Add()];//新增样式
style1.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style1.Font.Name = "宋体";//文字字体
style1.Font.Size = 12;//文字大小
//style1.Font.IsBold = true;//粗体
style1.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //应用边界线 左边界线
style1.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
style1.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //应用边界线 上边界线
style1.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //应用边界线 下边界线
style1.IsLocked = false;//单元格解锁
style1.ForegroundColor = Color.Aqua;// Color.Orange;//橙色
style1.Pattern = BackgroundType.Solid;
//档案数字化处理工作月统计表第一行
Cells cells1 = sheet.Cells;
//第一行高
cells1.SetRowHeight(0, 40);//设置行高
//cells1.ApplyStyle(style1, new StyleFlag() { All = true });
//excel数据列 循环列添加样式
for (int i = 0; i <= 17; i++)
{
cells1[0, i].SetStyle(style1);
cells1.SetColumnWidth(i, 10);
}
//第二行样式
Style style2 = TwoStyle(wb);
//添加数据单元格
Cells cells = sheet.Cells;
//访问表格中的“ a1”单元格。
Cell cell = sheet.Cells["A1"];
//创建数据表
DataTable dt = new DataTable();
//添加列名
for (int i = 0; i < wj.Length; i++)
{
dt.Columns.Add($"Column{i}", Type.GetType("System.String"));
}
//生成数据行
for (int i = 0; i < dt.Rows.Count; i++)
{
//第二行读取数据
int v = i;
//单元格
for (int k = 0; k < dt.Columns.Count; k++)
{
//值
string value = dt.Rows[i][k].ToString();
cells[v, k].PutValue(value);
//设置样式
cells[v, k].SetStyle(style2);
//行高
cells.SetRowHeight(v, 30);//设置行高
}
}