C#导出数据到Excel类

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.Windows.Forms;

namespace ExportExcelFile
{
	/// 
	/// 
	/// 
	public class Class1
	{
        	private Microsoft.Office.Interop.Excel.Application m_app;
		private Microsoft.Office.Interop.Excel.Workbook m_wb;
		private Microsoft.Office.Interop.Excel.Worksheet m_ws;
		//private Microsoft.Office.Interop.Excel.Range m_rang;
		/// 
		/// 
		/// 
		private void test()
		{
			Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

			if (xlApp == null)
			{
				Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
				return;
			}
			else
				Console.WriteLine("EXCEL started. Success");
			xlApp.Visible = true;
			Console.ReadLine();
			Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
			Worksheet ws = (Worksheet)wb.Worksheets[1];

			if (ws == null)
			{
				Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
			}
			else
				Console.WriteLine("Worksheet be started. Success");
			Console.ReadLine();
			// Select the Excel cells, in the range c1 to c7 in the worksheet.
			Range aRange = ws.get_Range("C1", "C7");

			if (aRange == null)
			{
				Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
			}
			else
				Console.WriteLine("Have get a range Success.");
			Console.ReadLine();
			// Fill the cells in the C1 to C7 range of the worksheet with the number 6.
			Object[] args = new Object[1];
			args[0] = 6;
			aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args);

			// Change the cells in the C1 to C7 range of the worksheet to the number 8.
			aRange.Value2 = 8;
		}
		/// 
		public void CreateSheet(object[] strHeads, object[][] strRows)
		{
			try
			{
				m_app = new Microsoft.Office.Interop.Excel.Application();
				if (m_app == null)
				{
					MessageBox.Show("无法打开Excel程序");
					return;
				}
				m_app.Visible = true;
				m_wb = m_app.Workbooks.Add(Missing.Value);
				m_ws = (Microsoft.Office.Interop.Excel.Worksheet)m_wb.ActiveSheet;
				m_ws.Visible = XlSheetVisibility.xlSheetVisible;
				for (int i = 1; i <= strHeads.Length; ++i)
				{
					m_ws.Cells[1, i] = strHeads[i - 1];
				}
				for (int j = 2; j <= strRows.Length + 1; ++j)
				{
					for (int i = 1; i <= strRows[j - 2].Length; ++i)
					{
						m_ws.Cells[j, i] = strRows[j - 2][i - 1];
					}

				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Source + ":" + ex.Message);
			}
		}

		public void CreateSheetEx(ListView listView1)
		{

			try
			{
				m_app = new Microsoft.Office.Interop.Excel.Application();
				if (m_app == null)
				{
					MessageBox.Show("无法打开Excel程序");
					return;
				}
				m_app.Visible = true;
				m_wb = m_app.Workbooks.Add(Missing.Value);
				m_ws = (Microsoft.Office.Interop.Excel.Worksheet)m_wb.ActiveSheet;
				m_ws.Visible = XlSheetVisibility.xlSheetVisible;
				for (int i = 1; i <= listView1.Columns.Count; ++i)
				{
					m_ws.Cells[1, i] = listView1.Columns[i - 1].Text;
				}
				for (int j = 2; j <= listView1.Items.Count + 1; ++j)
				{
					for (int i = 1; i <= listView1.Items[j-2].SubItems.Count; ++i)
					{
						m_ws.Cells[j, i] = listView1.Items[j-2].SubItems[i-1].Text;
					}

				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Source + ":" + ex.Message);
			}
		}
	}
}

		

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值