字符串格式直接生成Excel文件,不需要调用Office组件

 /// <summary>
        /// 字符串生成 Excel
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="saveFilePath"></param>
        public void StringSaveToExcel(DataTable dt, string saveFilePath, string strFactFileName, SqlParameter[] paraL)
        {
            try
            {
                int pagesize = 60000;//每个Excel最大行为60000
                StringBuilder sb = sb = new StringBuilder();
                string strFileName = paraL[5].Value.ToString();//显示的文件名
                int iRowCount = dt.Rows.Count;
                int count = 1;
                int pageIndex = 1;
                string saveExcelFileName = "";
                if (iRowCount < pagesize)
                {
                    saveExcelFileName = saveFilePath + strFactFileName + ".xls";
                }
                else
                {
                    saveExcelFileName = saveFilePath + strFactFileName + "_" + pageIndex.ToString() + ".xls";
                }
                //string saveExcelFileName = strFileName + "_" + pageIndex.ToString() + ".xls";
                foreach (DataRow dr in dt.Rows)
                {
                    string s = string.Empty;
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (s == string.Empty)
                        {
                            s = dr[i].ToString().Replace("\t", "").Replace("\n", "");
                        }
                        else
                        {
                            s += "\t" + dr[i].ToString().Replace("\t", "").Replace("\n", "");
                        }
                    }

                    sb.AppendLine(s);
                    count++;
                    if (count == pagesize ) //等于60000行的时候
                    {
                       
                        if (!File.Exists(saveExcelFileName))
                        {
                            WriteExcelFileHeader(saveExcelFileName, dt);
                        }
                     
                       
                        File.AppendAllText(saveExcelFileName, sb.ToString(), Encoding.Unicode);
                        sb = new StringBuilder();
                       
                    }
                    if (count == pagesize ) //
                    {
                        count = 1;
                        pageIndex++;
                        saveExcelFileName = saveFilePath + strFactFileName + "_" + pageIndex.ToString() + ".xls";
                    }
                }
                if (sb.Length > 0)
                {
                    if (!File.Exists(saveExcelFileName))
                    {
                        WriteExcelFileHeader(saveExcelFileName, dt);
                    }
                    
                    File.AppendAllText(saveExcelFileName, sb.ToString(), Encoding.Unicode);
                    sb = new StringBuilder();
                   
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 先写dt的列名
        /// </summary>
        /// <param name="strFile"></param>
        /// <param name="dt"></param>
        private void WriteExcelFileHeader(string saveExcelFileName, DataTable dt)
        {
            //File.Create(saveExcelFileName);//创建
            StringBuilder sb = new StringBuilder();
            string s = string.Empty;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (s == string.Empty)
                {
                    s = dt.Columns[i].ColumnName.Replace("\t", "").Replace("\n", "");
                }
                else
                {
                    s += "\t" + dt.Columns[i].ColumnName.Replace("\t", "").Replace("\n", "");
                }
            }

            sb.AppendLine(s);
            File.AppendAllText(saveExcelFileName, sb.ToString(), Encoding.Unicode);
        }


        /// <summary>
        /// 计算文件的大小
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        private decimal GetFileLength(string strPath)
        {
            if (!File.Exists(strPath)) //不存在文件,就返回0
            {
                return 0m;
            }
            try
            {
                FileInfo fInfo = new FileInfo(strPath);
                long size = fInfo.Length;   //   文件大小,单位是字节
                decimal FileLength = Convert.ToDecimal(size / 1024);
                FileLength = Math.Round(FileLength, 2);
                return FileLength;
            }
            catch
            {
                return 0m;
            }
        }

posted on 2008-03-26 17:54  KenL  阅读(301)  评论(0)    收藏  举报

导航