代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
using System.Reflection;
namespace UseingExcel
{
class ViewinExcel
{
public void TurnToExcel(ListView listView,string stname)
{
string Sheetname = stname;
ListView listView1 = listView;
if (listView1.Items.Count < 1)
return;
try
{
ExcelApplication MyExcel = new ExcelApplication();
MyExcel.Visible = true;
if (MyExcel == null)
{
return;
}
Workbooks MyWorkBooks = (Workbooks)MyExcel.Workbooks;
Workbook MyWorkBook = (Workbook)MyWorkBooks.Add(Missing.Value);
Worksheet MyWorkSheet = (Worksheet)MyWorkBook.Worksheets;
Range MyRange = MyWorkSheet.get_Range("A1","H1");
MyRange = MyRange.get_Resize(1, listView1.Columns.Count);
object MyHeader = new object[listView1.Columns.Count];
for (int i = 0; i < listView1.Columns.Count; i++)
{