博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataTable 导出excel 2003 采用ExcelLibrary
阅读量:5811 次
发布时间:2019-06-18

本文共 1698 字,大约阅读时间需要 5 分钟。

 public static   class ExportExcel

    {
        public static void DataTable2Excel(string attachName, DataTable tab)
        {
            string file = HttpContext.Current.Server.MapPath("/uploads/export/" + Guid.NewGuid().ToString() + ".xls");
            Workbook workbook = new Workbook();
            Worksheet worksheet = new Worksheet("Sheet1");
            //构建表头
            for (int col = 0; col < tab.Columns.Count; col++)
            {
                worksheet.Cells[0, col] = new Cell(tab.Columns[col].ColumnName);
            }
            for (int rowIndex = 0; rowIndex < tab.Rows.Count; rowIndex++)
            {
                for (int colIndex = 0; colIndex < tab.Columns.Count; colIndex++)
                {
                    worksheet.Cells[rowIndex + 1, colIndex] = new Cell(tab.Rows[rowIndex][colIndex].ToString());
                }
            }
            //设置默认宽度
            if (worksheet.Cells.LastRowIndex > 1)
            {
                for (int n = 0; n < tab.Columns.Count; n++)
                {
                    if (worksheet.Cells[1, n].Value.ToString().Length >= 50)
                        worksheet.Cells.ColumnWidth[(ushort)n] = 10000;
                    else if (worksheet.Cells[1, n].Value.ToString().Length >= 10)
                        worksheet.Cells.ColumnWidth[(ushort)n] = (ushort)(worksheet.Cells[1, n].Value.ToString().Length * 300);
                    else
                        worksheet.Cells.ColumnWidth[(ushort)n] = 3000;
                }
            }
            else
                worksheet.Cells.ColumnWidth[0, (ushort)(tab.Columns.Count - 1)] = 3000;
            workbook.Worksheets.Add(worksheet);
            workbook.Save(file);
            FileInfo info = new FileInfo(file);
            long size = 0;
            if (info.Exists)
                size = info.Length;
            HttpContext.Current.Response.AddHeader("content-type", "application/x-msdownload;");
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(attachName + ".xls"));
            HttpContext.Current.Response.AddHeader("content-length", size.ToString());
            HttpContext.Current.Response.WriteFile(file, 0, size);
        }
    }

转载于:https://www.cnblogs.com/xmyy/articles/2693181.html

你可能感兴趣的文章
SQL Server数据库概述
查看>>
Linux 目录结构及内容详解
查看>>
startx命令--Linux命令应用大词典729个命令解读
查看>>
华为3026c交换机配置tftp备份命令
查看>>
Oracle命令导入dmp文件
查看>>
OCP读书笔记(24) - 题库(ExamD)
查看>>
Http、TCP/IP协议与Socket之间的区别(转载)
查看>>
解决Unable to load R3 module ...VBoxDD.dll (VBoxDD):GetLastError=1790
查看>>
.net excel利用NPOI导入oracle
查看>>
vrpie在Visio Studio 中无法调试的问题
查看>>
第六课:数据库的基本工具
查看>>
关于二叉树重构的思索
查看>>
$_SERVER['SCRIPT_FLENAME']与__FILE__
查看>>
skynet实践(8)-接入websocket
查看>>
系统版本判断
查看>>
My97DatePicker 日历插件
查看>>
0603 学术诚信与职业道德
查看>>
小点心家族第3位成员——楼层定位效果
查看>>
Knockout.Js官网学习(enable绑定、disable绑定)
查看>>
hive基本操作与应用
查看>>