c# excel 表头设置

clyxy2008 2010-02-01 11:56:56
我用c#在excel表中导入了许多行数据,这就涉及到数据可能在好几页上,我想在后面的也上也加上个表头,例如“XXX项目数据”这样的题目,我看见microsoft office excel表格中的页面设置就可以设置表头,我想问引用的excel com组件里有这样的设置表头的函数吗,如果没有该怎么写
...全文
404 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
clyxy2008 2010-04-21
  • 打赏
  • 举报
回复
你没明白我的意思,你每页的表头是不变的,而我每页的表头是变化的,我想知道库函数有可以自己修改每页表头的参数功能没,不过你的方法我已经做过了,就是麻烦些,对每页的表头写函数处理在添加
clyxy2008 2010-02-02
  • 打赏
  • 举报
回复
没人知道吗,是不是已经很晚了
ctwei86 2010-02-02
  • 打赏
  • 举报
回复
导出方法,传入DataTable,和文件名。其中DataTable的列的ColumnName属性为最后写入Excel的表头。

public void Export(System.Data.DataTable dt, string filePath)
{
if (dt == null)
{
throw new Exception("数据表中无数据");
}
int eRowIndex = 1;
int eColIndex = 1;
int cols = dt.Columns.Count;
int rows = dt.Rows.Count;
Application xlApp = new ApplicationClass();
Workbook xlBook = xlApp.Workbooks.Add(true);
try
{
//列名的处理
for (int i = 0; i < cols; i++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Columns[i].ColumnName;
eColIndex++;
}
//列名加粗显示
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[eRowIndex, cols]).Font.Bold = true;
//xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Name = "Arial";
//xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Color = "#93CDDD";
//xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[eRowIndex, cols]).Font.Color = "#93CDDD";
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Size = "10";
eRowIndex++;

for (int i = 0; i < rows; i++)
{
eColIndex = 1;
for (int j = 0; j < cols; j++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Rows[i][j].ToString();
eColIndex++;
}
eRowIndex++;
}
//控制单元格中的内容。
xlApp.Cells.EntireColumn.AutoFit();
xlApp.DisplayAlerts = false;
xlBook.SaveCopyAs(filePath);
xlApp.Workbooks.Close();

}
catch
{
throw;
}
finally
{
xlApp.Quit();
//杀掉Excel进程。
GC.Collect();
}
}

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧