1. 程式人生 > >c# listview資料匯出到生成的excel檔案

c# listview資料匯出到生成的excel檔案

引用microsoft庫;
using Microsoft.Office.Interop.Excel;

private void button5_Click(object sender, EventArgs e)
{
if (listView1.Items == null) return;
string saveFileName = “”;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = “xls”;
saveDialog.Filter = “Excel檔案|*.xls”;
saveDialog.FileName = DateTime.Now.ToString(“yyyy-MM-dd”);
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(“:”) < 0)
return;
if (File.Exists(saveFileName)) File.Delete(saveFileName);
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show(“無法建立Excel物件,可能您的機器未安裝Excel”);
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
xlApp.Visible = false;
//填充列
for (int i = 0; i < listView1.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = listView1.Columns[i].Text.ToString();
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]).Font.Bold = true;
}
//填充資料(這裡分了兩種情況,1:lv帶CheckedBox,2:不帶CheckedBox)
//帶CheckedBoxes
if (listView1.CheckBoxes == true)
{
int tmpCnt = 0;
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
for (int j = 0; j < listView1.Columns.Count; j++)
{
if (j == 0)
{
worksheet.Cells[2 + tmpCnt, j + 1] =listView1.Items[i].Text.ToString();
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2 + tmpCnt, j + 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
}
else
{
worksheet.Cells[2 + tmpCnt, j + 1] = listView1.Items[i].SubItems[j].Text.ToString();
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2 + tmpCnt, j + 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
}
}
tmpCnt++;
}
}
}
else //不帶Checkedboxe
{
for (int i = 0; i < listView1.Items.Count; i++)
{
for (int j = 0; j < listView1.Columns.Count; j++)
{
if (j == 0)
{
worksheet.Cells[2 + i, j + 1] = listView1.Items[i].Text.ToString();
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2 + i, j + 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
}
else
{
worksheet.Cells[2 + i, j + 1] = listView1.Items[i].SubItems[j].Text.ToString();
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[2 + i, j + 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
}
}
}
}
object missing = System.Reflection.Missing.Value;
try
{
workbook.Saved = true;
workbook.SaveAs(saveFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
}
catch (Exception e1)
{
MessageBox.Show(“匯出檔案時出錯,檔案可能正被開啟!\n” + e1.Message);
}
finally
{
xlApp.Quit();
System.GC.Collect();
}
MessageBox.Show(“匯出Excle成功!”);
}