码迷,mamicode.com
首页 > 其他好文 > 详细

CreateExcel 导出Excel

时间:2014-06-09 19:07:56      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

bubuko.com,布布扣
    public class CreateExcel
    {

        /// <summary>
        /// 用Excel组件导出Excel文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="datable"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool SaveExcel(string path,DataSet datable,ref string msg)
        {

            Application exlApp = new ApplicationClass();

            if (exlApp == null)
            {
                msg = "Excel 未安装或无法启动!";
                return false;
            }

            Workbook exlworke = exlApp.Workbooks.Add(true);
            Worksheet exlworkSheet =(Worksheet)exlworke.ActiveSheet;

           
            int colunindex = 0;
            int rowindex = 0;
            int colunCount = datable.Tables[0].Columns.Count;
            int rowCount = datable.Tables[0].Rows.Count;

            Range exlrange = exlworkSheet.get_Range(exlApp.Cells[1, 1], exlApp.Cells[1, colunCount]);
            exlrange.MergeCells = true;
            exlApp.ActiveCell.FormulaR1C1 = "操作人: " + msg + "   导出时间:" + System.DateTime.Now.ToLongDateString();
            exlApp.ActiveCell.Font.Size = 10;
            exlApp.ActiveCell.Font.Bold = true;
            exlApp.ActiveCell.HorizontalAlignment = Constants.xlCenter;

            object[,] obneed = new object[rowCount + 1, colunCount + 1];

            foreach (DataColumn dc in datable.Tables[0].Columns)
            {
                obneed[rowindex, colunindex++] = dc.ColumnName;
            }
            rowindex++;

            foreach (DataRow dr in datable.Tables[0].Rows)
            {
                for(colunindex=0;colunindex<colunCount;colunindex++)
                {
                    obneed[rowindex, colunindex] = dr[colunindex];
                }
                rowindex++; 
            }

            exlrange = exlworkSheet.get_Range(exlApp.Cells[2, 1], exlApp.Cells[rowCount+2, colunCount]);//标题和标头+2
            exlrange.Value2 = obneed;

            try
            {
                exlworke.Saved = true;
                exlworke.SaveCopyAs(path);
            }
            catch
            {
                msg = "保存失败!";
            }
            finally
            {
                exlApp.Quit();
                GC.Collect();
            }
            msg = "导出成功!";
            return true;
        }
        /// <summary>
        /// 使用数据流导出Excel文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="datable"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool SaveIOExcel(string path, DataSet datable, ref string msg)
        {
         
            int colunindex = 0;
            int colunCount = datable.Tables[0].Columns.Count;
            int rowCount = datable.Tables[0].Rows.Count;
            try
            {
                using (FileStream flStream = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(flStream, System.Text.Encoding.GetEncoding(-0)))
                    {
                        StringBuilder strb = new StringBuilder();
                        for (colunindex = 0; colunindex < colunCount; colunindex++)
                        {
                            strb.Append(datable.Tables[0].Columns[colunindex].ColumnName).Append("\t");
                        }
                        strb.AppendLine();
                        foreach (DataRow dr in datable.Tables[0].Rows)
                        {
                            for (colunindex = 0; colunindex < colunCount; colunindex++)
                            {
                                strb.Append(dr[colunindex]).Append("\t");
                            }
                            strb.AppendLine();
                        }
                        strb.AppendLine("操作人: " + msg + "   导出时间:" + System.DateTime.Now.ToLongDateString());
                        sw.Write(strb.ToString());
                    }

                }
            }
            catch
            {
                msg = "转换失败!";
                return false;
            }
            msg = "导出成功!";
            return true;
        }
    }
bubuko.com,布布扣

 

CreateExcel 导出Excel,布布扣,bubuko.com

CreateExcel 导出Excel

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/dragon-L/p/3777217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!