//接收一个ListView将全部数据导入EXCEL
public void OperateExcel(ListView list, string text)
{
btnTotalExcel.Enabled = false;
//text为些Excel的标题
Microsoft.Office.Interop.Excel.Application ss = new Microsoft.Office.Interop.Excel.Application();//开启Excel
ss.Application.Workbooks.Add(true);
ss.Visible = true;
ss.Cells[1, 4] = text;
for (int x = 1; x <= list.Columns.Count; x++)
{
ss.Rows.Cells[2, x] = list.Columns[x - 1].Text;
}
for (int i = 3; i <= list.Items.Count + 2; i++)
{
for (int j = 1; j <= list.Columns.Count; j++)
{
ss.Rows.Cells[i, j] = list.Items[i - 3].SubItems[j - 1].Text + ",";
}
}
btnTotalExcel.Enabled = true;
}
//接收一个ListView将其中选中数据导入EXCEL
public void OperatePostExcel(ListView list, string text)
{
btnPartExel.Enabled = false;
Microsoft.Office.Interop.Excel.Application ss = new Microsoft.Office.Interop.Excel.Application();
ss.Application.Workbooks.Add(true);
ss.Visible = true;
ss.Cells[1, 4] = text;
for (int x = 1; x <= list.Columns.Count; x++)
{
ss.Rows.Cells[2, x] = list.Columns[x - 1].Text;
}
for (int i = 3; i <= list.SelectedItems.Count + 2; i++)
{
for (int j = 1; j <= list.Columns.Count; j++)
{
ss.Rows.Cells[i, j] = list.SelectedItems[i - 3].SubItems[j - 1].Text + ",";
}
}
btnPartExel.Enabled = true;
}