datatable 导出 Excel时,怎么操作Excel的表格宽度

2024-12-25 11:03:38
推荐回答(1个)
回答1:

StringBuilder strHtml = new StringBuilder();
strHtml.Append("");
strHtml.Append("

");
strHtml.Append("");
strHtml.Append("");
for (int i = 0; i < dgv.Columns.Count; i++)
{
strHtml.Append("");
}
strHtml.Append("");
for (int i = 0; i < dgv.Rows.Count; i++)
{
strHtml.Append("");
for (int j = 0; j < dgv.Columns.Count; j++)
{
string td = "";
strHtml.Append(td);
}
strHtml.Append("");
}
strHtml.Append("
" + m_strTitle + "
" + dgv.Columns[i].HeaderText + "
" + (dgv.Rows[i].Cells[j].Value != null ? dgv.Rows[i].Cells[j].Value.ToString() : "") + "
");
System.IO.File.WriteAllBytes(m_strFilename, System.Text.Encoding.UTF8.GetBytes(strHtml.ToString()));

-------------------------
修改上面的 td th 等等,定义即可.
dgv 是DataGridView 控件. 都是类似的