当前位置:编程学习 > C#/ASP.NET >>

如何批量下载数据库中的记录

每条记录生成一个word文件,然后批量下载,如何实现呢?使用asp.net + oracle,最好能以压缩包的形式下载,不知道是否能够实现 --------------------编程问答-------------------- 补充一下:插叙符合条件的记录,然后每条记录生成一个word文件,最后以压缩包的形式下载,不知道能否实现,请大侠帮忙,谢谢 --------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 谢谢顶贴,继续关注 --------------------编程问答-------------------- 为什么要生成单个word呢?可以生成以“,”号分隔的csv文件,文件压缩后又小,又好处理,可以用web服务或wcf


http://www.mybuffet.cn --------------------编程问答-------------------- 需要的就是这个功能啊,为了满足别人的要求啊,我也没做过类似的功能,所以请教高手啊,不知道容易实现吗? --------------------编程问答-------------------- 插叙符合条件的记录,然后每条记录生成一个word文件,最后以压缩包的形式下载,不知道能否实现

先把文件保存到文件夹。然后打包文件夹这个是可以做到的。


using System;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
 
using System.IO;
using System.IO.Compression;

namespace GzipHelper
{
    public class ZipHelper
    {
        public static bool CompressFile(string sourceFile, string destinationFile)
        {
            bool isSuccess = false;
            // make sure the source file is there
            if (File.Exists(sourceFile) == false)
                throw new FileNotFoundException();

            // Create the streams and byte arrays needed
            byte[] buffer = null;
            FileStream sourceStream = null;
            FileStream destinationStream = null;
            GZipStream compressedStream = null;

            try
            {
                // Read the bytes from the source file into a byte array
                sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);

                // Read the source stream values into the buffer
                buffer = new byte[sourceStream.Length];
                int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);

                if (checkCounter != buffer.Length)
                {
                    throw new ApplicationException();
                }

                // Open the FileStream to write to
                destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write);

                // Create a compression stream pointing to the destiantion stream
                compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true);

                // Now write the compressed data to the destination file
                compressedStream.Write(buffer, 0, buffer.Length);
                isSuccess = true;
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }
            finally
            {
                // Make sure we allways close all streams
                if (sourceStream != null)
                    sourceStream.Close();

                if (compressedStream != null)
                    compressedStream.Close();

                if (destinationStream != null)
                    destinationStream.Close();
            }
            return isSuccess;
        }
        public static bool DecompressFile(string sourceFile, string destinationFile)
        {
            bool isSuccess = false;
            // make sure the source file is there
            if (File.Exists(sourceFile) == false)
                throw new FileNotFoundException();

            // Create the streams and byte arrays needed
            FileStream sourceStream = null;
            FileStream destinationStream = null;
            GZipStream decompressedStream = null;
            byte[] quartetBuffer = null;

            try
            {
                // Read in the compressed source stream
                sourceStream = new FileStream(sourceFile, FileMode.Open);

                // Create a compression stream pointing to the destiantion stream
                decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);

                // Read the footer to determine the length of the destiantion file
                quartetBuffer = new byte[4];
                int position = (int)sourceStream.Length - 4;
                sourceStream.Position = position;
                sourceStream.Read(quartetBuffer, 0, 4);
                sourceStream.Position = 0;
                int checkLength = BitConverter.ToInt32(quartetBuffer, 0);

                byte[] buffer = new byte[checkLength + 100];

                int offset = 0;
                int total = 0;



--------------------编程问答--------------------
补全

                // Read the compressed data into the buffer
                while (true)
                {
                    int bytesRead = decompressedStream.Read(buffer, offset, 100);

                    if (bytesRead == 0)
                        break;

                    offset += bytesRead;
                    total += bytesRead;
                }

                // Now write everything to the destination file
                destinationStream = new FileStream(destinationFile, FileMode.Create);
                destinationStream.Write(buffer, 0, total);

                // and flush everyhting to clean out the buffer
                destinationStream.Flush();
                isSuccess = true;
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }
            finally
            {
                // Make sure we allways close all streams
                if (sourceStream != null)
                    sourceStream.Close();

                if (decompressedStream != null)
                    decompressedStream.Close();

                if (destinationStream != null)
                    destinationStream.Close();
            }
            return isSuccess;
        }

        public static bool CompressFolder(string dirPath, string fileName)
        {
            bool isSuccess = false;
            try
            {
                SerializeFileInfo list = getStream(dirPath);
                IFormatter formatter = new BinaryFormatter();
                using (Stream s = new MemoryStream())
                {
                    formatter.Serialize(s, list);
                    s.Position = 0;
                    CreateCompressFile(s, fileName);
                    isSuccess = true;
                }
            }
            catch (ApplicationException ex)
            {
                isSuccess = false;
            }
            finally
            {

            }
            return isSuccess;
        }
        public static bool DecompressFolder(string fileName, string dirPath)
        {
            bool isSuccess = false;
            try
            {
                using (Stream source = File.OpenRead(fileName))
                {
                    using (Stream destination = new MemoryStream())
                    {
                        using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true))
                        {
                            byte[] bytes = new byte[4096];
                            int n;
                            while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
                            {
                                destination.Write(bytes, 0, n);
                            }
                        }
                        destination.Flush();
                        destination.Position = 0;
                        DeSerializeFiles(destination, dirPath);
                    }
                }
            }
            catch (ApplicationException ex)
            {
                isSuccess = false;
            }
            finally
            {

            }
            return isSuccess;
        }
 

        private static SerializeFileInfo getStream(string dirPath)
        {
            SerializeFileInfo list = new SerializeFileInfo("", "folder", null);
            list.FileName = dirPath.Substring(dirPath.LastIndexOf(Path.DirectorySeparatorChar) + 1);
            foreach (string f in Directory.GetFiles(dirPath))
            {
                byte[] destBuffer = File.ReadAllBytes(f);
                SerializeFileInfo sfi = new SerializeFileInfo(f,"file", destBuffer);
                list.Add(sfi);
            }
            foreach (string f in Directory.GetDirectories(dirPath))
            {
                SerializeFileInfo sfi = getStream(dirPath + "\\" + f.Substring(f.LastIndexOf(Path.DirectorySeparatorChar)+1));
                sfi.Type = "folder";
                list.Add(sfi);
            }
            return list;
        }

      
        private static void DeSerializeFiles(Stream s, string dirPath)
        {
            BinaryFormatter b = new BinaryFormatter();
            ArrayList list = (ArrayList)b.Deserialize(s);
 
            setStream(list,dirPath);
        }

        private static void setStream(ArrayList list,string dirPath)
        {
            foreach (SerializeFileInfo f in list)
            {
                if (f.Type == "file")
                {
                    SerializeFileInfo sfi = f as SerializeFileInfo;
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }
                    string newName = dirPath + "\\" + Path.GetFileName(sfi.FileName);
                    using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write))
                    {
                        fs.Write(sfi.FileBuffer, 0, sfi.FileBuffer.Length);
                        fs.Close();
                    }
                }
                if (f.Type == "folder")
                {
                    setStream(f, dirPath + "\\" + f.FileName);
                }
            }
        }

        private static void CreateCompressFile(Stream source, string destinationName)
        {
            using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write))
            {
                using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
                {
                    byte[] bytes = new byte[4096];
                    int n;
                    while ((n = source.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        output.Write(bytes, 0, n);
                    }
                }
            }
        }

        [Serializable]
        private class SerializeFileInfo : ArrayList
        {
            public SerializeFileInfo(string name, string type, byte[] buffer)
            {
                fileName = name;
                fileBuffer = buffer;
                strType = type;
            }

            string fileName;
            string strType;
            public string FileName
            {
                get
                {
                    return fileName;
                }
                set
                {
                    fileName = value;
                }
            }
            public string Type
            {
                get
                {
                    return strType;
                }
                set
                {
                    strType = value;
                }
            }

            byte[] fileBuffer;
            public byte[] FileBuffer
            {
                get
                {
                    return fileBuffer;
                }
            }
        }

    }
}

--------------------编程问答-------------------- 思路:读取一条记录-》生成文件(循环)-》利用ICSharpCode.SharpZipLib.dll压缩所有文件(方法http://blog.csdn.net/polarissky/archive/2009/12/14/5005030.aspx) --------------------编程问答-------------------- 用程序根据遍历的结果生成一个.zip文件如可使用gzip,
SharpZipLib,winrar然后再下载.  --------------------编程问答-------------------- 怎样写入word效果比较好呢? --------------------编程问答-------------------- 每一个记录都是一个文件,我靠,这么强大,什么需求,不会是楼主你自己想出来的吧。 --------------------编程问答--------------------

想法不错,帮你了 --------------------编程问答-------------------- 我可没时间胡思乱想,是上帝要求的功能,求助了啊 --------------------编程问答-------------------- Up
引用 8 楼 polarissky 的回复:
思路:读取一条记录-》生成文件(循环)-》利用ICSharpCode.SharpZipLib.dll压缩所有文件(方法http://blog.csdn.net/polarissky/archive/2009/12/14/5005030.aspx)
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,