c# 导出数据到 excel

46 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.IO;






namespace meifengSystem
{
    class ExportExcel
    {
         private string filePath = "";
         public ExportExcel(string excel_path) 
        { 
                // 
                // TODO: 在此处添加构造函数逻辑 
                // 
                filePath = excel_path; 
        } 
        /// <summary> 
        /// 将指定的Dataset导出到Excel文件 
        /// </summary> 
        /// <param name="dt"></param> 
        /// <returns></returns> 
        public bool ExportToExcel(System.Data.DataSet ds, string ReportName) 
        { 
            if (ds.Tables[0].Rows.Count == 0) 
            { 
                MessageBox.Show("数据集为空"); 
            } 
            Microsoft.Office.Interop.Excel._Application xlapp = new ApplicationClass(); 
            Workbook xlbook = xlapp.Workbooks.Add(true); 
            Worksheet xlsheet = (Worksheet)xlbook.Worksheets[1]; 
            Range range = xlsheet.get_Range(xlapp.Cells[1, 1], xlapp.Cells[1, ds.Tables[0].Columns.Count]); 
            range.MergeCells = true; 
            xlapp.ActiveCell.FormulaR1C1 = ReportName; 
            xlapp.ActiveCell.Font.Size = 20; 
            xlapp.ActiveCell.Font.Bold = true; 
            xlapp.ActiveCell.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter; 
            int colIndex = 0; 
            int RowIndex = 2; 
            //开始写入每列的标题 
            foreach (DataColumn dc in ds.Tables[0].Columns) 
            { 
                colIndex++; 
                xlsheet.Cells[RowIndex, colIndex] = dc.Caption; 
            } 
            //开始写入内容 
            int RowCount = ds.Tables[0].Rows.Count;//行数 
            for (int i = 0; i < RowCount; i++) 
            { 
                RowIndex++; 
                int ColCount = ds.Tables[0].Columns.Count;//列数 
                for (colIndex = 1; colIndex <= ColCount; colIndex++) 
                { 
                    xlsheet.Cells[RowIndex, colIndex] = ds.Tables[0].Rows[i][colIndex - 1];//dg[i, colIndex - 1]; 
                    xlsheet.Cells.ColumnWidth = ds.Tables[0].Rows[i][colIndex - 1].ToString().Length; 
                } 
            } 


            xlbook.Saved = true; 
            xlbook.SaveCopyAs(filePath); 
            xlapp.Quit(); 
            GC.Collect(); 
            return true; 
        } 


        public bool ExportToExcelOF(System.Data.DataSet ds, string ReportName) 
        { 
            if (ds.Tables[0].Rows.Count == 0) 
            { 
                MessageBox.Show("数据集为空"); 
            } 
            string FileName = filePath; 


            //System.Data.DataTable dt = new System.Data.DataTable(); 
            FileStream objFileStream; 
            StreamWriter objStreamWriter; 
            string strLine = ""; 
            objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write); 
            objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode); 


            strLine = ReportName; 
            objStreamWriter.WriteLine(strLine); 
            strLine = ""; 


            for (int i = 0; i < ds.Tables[0].Columns.Count; i++) 
            { 
                strLine = strLine + ds.Tables[0].Columns[i].ColumnName.ToString() + "          " + Convert.ToChar(9); 
            } 
            objStreamWriter.WriteLine(strLine); 
            strLine = ""; 


            for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
            { 
                strLine = strLine + (i + 1) + Convert.ToChar(9); 
                for (int j = 1; j < ds.Tables[0].Columns.Count; j++) 
                { 
                    strLine = strLine + ds.Tables[0].Rows[i][j].ToString() + Convert.ToChar(9); 
                } 
                objStreamWriter.WriteLine(strLine); 
                strLine = ""; 
            } 
            objStreamWriter.Close(); 
            objFileStream.Close(); 


            //Microsoft.Office.Interop.Excel._Application xlapp = new ApplicationClass(); 
            //Workbook xlbook = xlapp.Workbooks.Add(true); 
            //Worksheet xlsheet = (Worksheet)xlbook.Worksheets[1]; 
            //Range range = xlsheet.get_Range(xlapp.Cells[1, 1], xlapp.Cells[1, ds.Tables[0].Columns.Count]); 
            //range.EntireColumn.AutoFit(); 
            //xlapp.Quit(); 
            return true; 
        }      
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值