gpt4 book ai didi

c# - 数据 GridView 导出到excel

转载 作者:太空狗 更新时间:2023-10-29 21:41:39 27 4
gpt4 key购买 nike

我可以将 datagridview 的数据导出到 excel。但是没有导出 datagridview 的实际格式,即字体、颜色和空间。那么,有没有什么最好的方法可以将 datagridview 导出到 excel,即不仅是数据,还有外观。

示异常(exception)观是这样的:

enter image description here

最佳答案

尝试 CSV 导出

private void ToCsV(DataGridView dGV, string filename)
{
string separator = ",";
StringBuilder stOutput = new StringBuilder();
// Export titles:
StringBuilder sHeaders = new StringBuilder();
for (int j = 0; j < dGV.Columns.Count; j++)
{
sHeaders.Append(dGV.Columns[j].HeaderText);
sHeaders.Append(separator);
}
stOutput.AppendLine(sHeaders.ToString());
// Export data.
for (int i = 0; i < dGV.RowCount - 1; i++)
{
StringBuilder stLine = new StringBuilder();
for (int j = 0; j < dGV.ColumnCount; j++)
{
stLine.Append(Convert.ToString(dGV[j, i].Value));
stLine.Append(separator);
}
stOutput.AppendLine(stLine.ToString());
}

File.WriteAllText(filename, stOutput.ToString());
}

关于c# - 数据 GridView 导出到excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6196770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com