搜索
您的当前位置:首页正文

OpenXML方式输出到Excel(考虑超百万)

来源:步旅网
        /// <summary>
        /// 输出到单个Excel文件,考虑超100万的数据 2019.12.24
        /// </summary>
        /// <param name="config"></param>
        /// <param name="xlsxFilename"></param>
        /// <param name="dir"></param>
        public void Output(ImportConfig config, string xlsxfilename, string dir = "")
        {
            bool hasTitle = true;
            int headLineCount = 0;

            int batchCount = 1000000;

            uint shtId = 1;
            int pageNo = 0, pageCount = 0, total = 0;

            Dictionary<string, string> dict = this.GetItems();

            if (string.IsNullOrEmpty(dir)) dir = MyCommon.GetDeskTopTimeDir();
            xlsxfilename = Path.Combine(dir, xlsxfilename);

            using (SpreadsheetDocument doc = SpreadsheetDocument.Create(xlsxfilename, SpreadsheetDocumentType.Workbook))
            {
                doc.AddWorkbookPart();
                List<Sheet> shts = new List<Sheet>();
                OpenXmlWriter writer = null;

                foreach (var item in dict)
                {
                    using (DbConnection conn = DbFactory.GetConnection(config))
                    {
                        conn.Open();
                        DbCommand cmd = DbFactory.GetCommand(config, item.Value, conn);
                        DbDataReader reader = cmd.ExecuteReader();

                        bool pageStart = true;


                        while (reader.Read())
                        {
                            if (pageStart)
                            {
                                pageStart = false;

                                writer = WriteWorksheetPartStart(doc, shts, item.Key, pageNo, shtId);

                                if (hasTitle)
                                {
                                    headLineCount = 1;
                                    WriteHeadLine(writer, reader, headLineCount);
                                }


                            }

                            WriteLine(writer, reader, 1 + pageCount + headLineCount);

                            total++; pageCount++;
                            if

因篇幅问题不能全部显示,请点此查看更多更全内容

Top