AppLibrary.dll中对Excel下载的用法

来源:互联网 发布:淘宝上的aj是正品吗 编辑:程序博客网 时间:2024/05/24 04:11

此类库文件,用于页面组织Excel表格数据并下载Excel的用法:

AppLibrary.Bits.Bytes bytes = new AppLibrary.Bits.Bytes();  //定义字节数据
AppLibrary.WriteExcel.XlsDocument doc = new AppLibrary.WriteExcel.XlsDocument();
doc.FileName = string.Format("新Excel_{0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));  //EXcel名称
string fileName = doc.FileName;


Response.Buffer = false;
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentType = "application/octet-stream";
Response.Charset = "utf-8";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);  //客户端下载时显示的名称


AppLibrary.WriteExcel.Worksheet sheet = doc.Workbook.Worksheets.Add("Sheet名称"); //Sheet名称
AppLibrary.WriteExcel.Cells cells = sheet.Cells;  //为Sheet定义单元格


AppLibrary.WriteExcel.ColumnInfo colInfo = new AppLibrary.WriteExcel.ColumnInfo(doc, sheet);    //为Excel设置列宽
colInfo.ColumnIndexStart = 0;  //开始列  
colInfo.ColumnIndexEnd = 10;   //结束列
colInfo.Width = 800;   //列宽
colInfo.Collapsed = true;
sheet.AddColumnInfo(colInfo);


//单元格格式设置
AppLibrary.WriteExcel.XF XFstyle = doc.NewXF();
XFstyle.HorizontalAlignment = AppLibrary.WriteExcel.HorizontalAlignments.Centered;   //设置左右对齐
XFstyle.Font.FontName = "宋体";  //字体
XFstyle.Font.Height = 230;
XFstyle.Font.Bold = true;   //字体加宽
XFstyle.VerticalAlignment = VerticalAlignments.Centered;  //设置上下对齐
XFstyle.Pattern = 1;  //背景色(与下共用)

XFstyle.PatternColor = Colors.Green; //背景色(与上共用)

XFstyle.TopLineStyle = 1;  //单元格边框色(与下共用)

XFstyle.TopLineColor = Colors.Black;   //单元格边框色(与上共用)

        
cells.Add(1, 1, "文本", XFstyle); //为一行一格添加文本与属性
cells.Add(1, 2, "文本", XFstyle); //为一行二格添加文本与属性
cells.Add(2, 1, "文本", XFstyle); //为二行一格添加文本与属性

cells.Add(2, 2, "文本", XFstyle);

cells.Merge(3,3,1,2);  //合并单元格



var statement = _Service.GetList().Where(c => c.ID == id);  //获取数据集
int f = 3;
foreach (var temp in statement)  //使用foreach为单元格赋值
{
    f++;
    cells.Add(f, 1, temp.ID);
    cells.Add(f, 2, temp.Name);
    cells.Add(f, 3, temp.Type);
    cells.Add(f, 4, temp.Remark);
}


bytes = doc.Bytes;
doc = null;
GC.Collect();  //回收


//写入客户端
long length = bytes.Length;
Response.AddHeader("Content-Length", length.ToString());
int bufferSize = 100000;
byte[] Buffer = new Byte[bufferSize];      //存放欲发送数据的缓冲区  


int ByteToRead;                           //每次实际读取的字节数  
for (int i = 0; i <= length / bufferSize; i++)
{
    //客户端浏览器还打开着,继续传送  
    if (Response.IsClientConnected)
    {


        if (i == length / bufferSize)
        {
            Buffer = bytes.Get(i * bufferSize, Convert.ToInt32(length - i * bufferSize)).ByteArray;
        }
        else
        {
            Buffer = bytes.Get(i * bufferSize, bufferSize).ByteArray;
        }
        ByteToRead = Buffer.Length;                   //往缓冲区读入数据  
        Response.OutputStream.Write(Buffer, 0, ByteToRead);   //把缓冲区的数据写入客户端浏览器  
        Response.Flush();         //立即写入客户端  
        length -= ByteToRead;     //剩余字节数减少  
    }
    else
    {
        break;
    }
}

Response.End();  //发送结束


其他未补充的,还未探究,希望有人帮我继续完善。

0 0