首页 > 代码库 > CreateExcel 导出Excel

CreateExcel 导出Excel

    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;
        }
    }