我正在尝试将listview导出到excel文件。列表视图中的信息已成功导出,但列不是。这是代码:
StringBuilder sb = new StringBuilder();
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = true;
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(1);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
int i = 1;
int i2 = 1;
int x = 1;
int x2 = 1;
foreach (ColumnHeader ch in listView1.Columns)
{
ws.Cells[x2, x] = ch.Text;
x++;
}
foreach (ListViewItem lvi in listView1.Items)
{
i = 1;
foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
{
ws.Cells[i2, i] = lvs.Text;
ws.Cells.Select();
ws.Cells.EntireColumn.AutoFit();
i++;
}
i2++;
}
答案 0 :(得分:1)
您可以尝试设置i2 = 2
而不是i2 = 1
吗?可能是,第二部分因为i2 = 1
而覆盖了列标题。