求datatable或dataset导出到Excel的代码,要那种能自由设置每列样式和列名。
1个回答
展开全部
System.IO.StreamWriter sw = new System.IO.StreamWriter(FileName, false, System.Text.Encoding.GetEncoding("GB2312"));
object[] values = new object[table.Columns.Count];
string headName = "";
for (int i = 0; i < table.Columns.Count; ++i)
{
sw.Write(table.Columns[i].Caption.ToString());
sw.Write('\t');
}
sw.Write("\r\n");
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < values.Length; j++)
{
headName = table.Rows[i][j].ToString().Trim();
sw.Write(headName);
sw.Write('\t');
}
sw.Write("\r\n");
}
sw.Flush();
sw.Close();
object[] values = new object[table.Columns.Count];
string headName = "";
for (int i = 0; i < table.Columns.Count; ++i)
{
sw.Write(table.Columns[i].Caption.ToString());
sw.Write('\t');
}
sw.Write("\r\n");
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < values.Length; j++)
{
headName = table.Rows[i][j].ToString().Trim();
sw.Write(headName);
sw.Write('\t');
}
sw.Write("\r\n");
}
sw.Flush();
sw.Close();
追问
我要那种能自由设置每列样式和列名的
追答
那你处理下就可以了,你要什么列名就在第一个循环里处理,样式呢就看你要什么样的了 也可以处理,我的这个是导出去一个默认样式的excel,要改变默认的就需要增加代码去处理,
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询