日期:2014-05-18 浏览次数:20659 次
using System;
using System.Data;
using Sweecom.BLL;
using System.Collections;
using System.Windows.Forms;
using Sweecom.Model;
using function;
namespace Web.function
{
    /// <summary>
    /// MakeReport 的摘要说明。
    /// </summary>
    public class MakeReport
    {
        public MakeReport()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        private System.Data.DataTable _dt;
        public System.Data.DataTable dt
        {
            get{return _dt;}
            set{_dt = value;}
        }
        private string _strFilePath;
        public string strFilePath
        {
            get{return _strFilePath;}
            set{_strFilePath = value;}
        }
        private String[] _columnNames;
        public String[] ColumnNames
        {
            get { return _columnNames; }
            set { _columnNames = value; }
        }
        public bool CreatFile()
        {
            bool cFile = false;;
            //            try
            //            {
            Microsoft.Office.Interop.Excel.ApplicationClass oExcel;
                
            oExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            oExcel.UserControl = false;
            Microsoft.Office.Interop.Excel.WorkbookClass wb = (Microsoft.Office.Interop.Excel.WorkbookClass)oExcel.Workbooks.Add(System.Reflection.Missing.Value);
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                oExcel.Cells[1, j + 1] = _dt.Columns[j].ToString();
            }
            for (int i = 0; i < _dt.Rows.Count; i++)
            {
                DataRow dr = _dt.Rows[i];
                for (int j = 0; j < _dt.Columns.Count; j++)
                {
                    
                        
                    oExcel.Cells[i + 2, j + 1] = dr[dt.Columns[j].ToString()].ToString();
                        
                }
            }