Example #1
0
        public static void ExportToCsv(System.Web.HttpResponse response, DataTable exportData, string exportName)
        {
            response.Clear();
            byte[] BOM = { 0xEF, 0xBB, 0xBF }; // UTF-8 BOM karakterleri
            response.BinaryWrite(BOM);
            StringBuilder sb = new StringBuilder();
            foreach (DataColumn dc in exportData.Columns)
            {
                sb.Append((char)(34) + dc.ColumnName + (char)(34));
                sb.Append(";");
            }
            sb = sb.Remove(sb.Length - 1, 1);
            sb.AppendLine();
            foreach (DataRow dr in exportData.Rows)
            {

                for (int i = 0; i < exportData.Columns.Count; i++)
                {
                    sb.Append(dr[i].ToString().Replace(';', ',').Replace('\n', ' ').Replace('\t', ' ').Replace('\r', ' '));
                    sb.Append(";");
                }
                sb = sb.Remove(sb.Length - 1, 1);
                sb.AppendLine();
            }
            response.ContentType = "text/csv";
            response.AppendHeader("Content-Disposition", "attachment; filename=" + exportName + ".csv");
            response.Write(sb.ToString());
            response.End();
        }
 /// <summary>
 /// 把DataTable导出为Word文件
 /// </summary>
 /// <param name="page">Page</param>
 /// <param name="fileName">Word文件名(不包括后缀*.doc)</param>
 /// <param name="dtbl">将要被导出的DataTable对象</param>
 /// <returns></returns>
 public static bool DataTableToWord(System.Web.HttpResponse response, string fileName, DataTable dtbl)
 {
     response.Clear();
     response.Buffer = true;
     response.Charset = "UTF-8";
     response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".doc");
     response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
     response.ContentType = "application/ms-word";
     //page.EnableViewState = false;
     response.Write(DataTableToHtmlTable(dtbl));
     response.End();
     return true;
 }
        private bool DownFile(System.Web.HttpResponse Response, string fileName, string fullPath)
        {
            try
            {
                Response.ContentType = "application/octet-stream";

                Response.AppendHeader("Content-Disposition", "attachment;filename=" +
                HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312");
                System.IO.FileStream fs = System.IO.File.OpenRead(fullPath);
                long fLen = fs.Length;
                int size = 102400;//每100K同时下载数据 
                byte[] readData = new byte[size];//指定缓冲区的大小 
                if (size > fLen) size = Convert.ToInt32(fLen);
                long fPos = 0;
                bool isEnd = false;
                while (!isEnd)
                {
                    if ((fPos + size) > fLen)
                    {
                        size = Convert.ToInt32(fLen - fPos);
                        readData = new byte[size];
                        isEnd = true;
                    }
                    fs.Read(readData, 0, size);//读入一个压缩块 
                    Response.BinaryWrite(readData);
                    fPos += size;
                }
                fs.Close();
                System.IO.File.Delete(fullPath);
                return true;
            }
            catch
            {
                return false;
            }
        }
Example #4
0
        public void Save(System.IO.Stream OutputStream, System.Web.HttpResponse response)
        {
            string filename = "Export_" + Guid.NewGuid().ToString();
            string completePathOne = GenerateUniquePath(filename, "xlsx");
            string completePathTwo = GenerateUniquePath(filename, "xls");
            string completePath;
            string tableName;

            ISDWorksheet ws = (ISDWorksheet)this.Worksheets[0];
            ISDTable ta = ws.Table;
            tableName = ws.Name;
            ArrayList rows = ta.Rows;
            ISDWorksheetRow row0 = null;

            if (rows.Count > 0) {
                row0 = (ISDWorksheetRow)rows[0];
            }

            ISDWorksheetRow row1 = row0;

            if (rows.Count > 1) {
                row1 = (ISDWorksheetRow)rows[1];
            }

            ArrayList cols = ta.Columns;
            string colDefs = GetColumnDefinitions(cols, row0.Cells, row1.Cells, true);
            string colNames = GetColumnDefinitions(cols, row0.Cells, row1.Cells, false);

            completePath = completePathTwo;

            HSSFWorkbook hssfwb = new HSSFWorkbook();

            Sheet sh = hssfwb.CreateSheet("Sheet1");

            int rIndex = 0;

            Row r = sh.CreateRow(rIndex);

            int c = 0;

            foreach (ISDWorksheetCell hCell in row0.Cells)
            {
                Cell ce = r.CreateCell(c);

                ce.SetCellValue(hCell.Text);

                HSSFCellStyle style = (HSSFCellStyle)hssfwb.CreateCellStyle();
                style.WrapText = true;
                ce.CellStyle = style;

                c++;
            }

            string myValue = "";

            for (rIndex = 1; rIndex < rows.Count; rIndex++)
            {
                ISDWorksheetRow currentRow = (ISDWorksheetRow)rows[rIndex];

                r = sh.CreateRow(rIndex);
                c = 0;

                foreach (ISDWorksheetCell dCell in currentRow.Cells)
                {
                    myValue = dCell.Text.Replace("'", "''");

                    if (myValue.Length > 255)
                    {
                        myValue = myValue.Substring(0, 255);
                    }

                    Cell ce = r.CreateCell(c);

                    ce.SetCellValue(myValue);

                    HSSFCellStyle style = (HSSFCellStyle)hssfwb.CreateCellStyle();
                    style.WrapText = true;
                    ce.CellStyle = style;

                    c++;
                }
            }

            MemoryStream ms = new MemoryStream();
            hssfwb.Write(ms);

            string NPOIDownloadFileName = this.Properties.Title;

            if (completePath.EndsWith(".xlsx"))
            {
                NPOIDownloadFileName += ".xlsx";
            }
            else
            {
                NPOIDownloadFileName += ".xls";
            }

            response.ClearHeaders();
            response.Clear();
            response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
            response.Cache.SetMaxAge(new TimeSpan(0));
            response.Cache.SetExpires(new DateTime(0));
            response.Cache.SetNoServerCaching();
            response.AppendHeader("Content-Disposition", ("attachment; filename=\"" + (NPOIDownloadFileName + "\"")));
            response.ContentType = "application/vnd.ms-excel";

            OutputStream.Write(ms.ToArray(), 0, ms.ToArray().Length);

            return;
        }
Example #5
0
 // SetsResponse header and cache
 public void SetupResponse(System.Web.HttpResponse response, string fileName)
 {
     response.ClearHeaders();
     response.Clear();
     response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
     response.Cache.SetMaxAge(new TimeSpan(0));
     response.Cache.SetExpires(new DateTime(0));
     response.Cache.SetNoServerCaching();
     response.AppendHeader("Content-Disposition", ("attachment; filename=\"" + (fileName + "\"")));
 }
Example #6
0
 public static void ExportToExcel(System.Web.HttpResponse Response, string datos)
 {
     Response.Clear();
     Response.Write(datos);
     Response.ContentType = "application/vnd.ms-excel";
     Response.AppendHeader("Content-Disposition", "attachment; filename=Informe.xls");
     Response.End();
 }