关于Excel操作

天涯孤岸软件商城,.net电子商务商城源码,,以及其它源码, 欢迎访问:http://tyga.tygaweb.com/ (新)
 
使用javascript:Office JavaScript API library
https://appsforoffice.microsoft.com/lib/1.0/hosted/office.js

参考文章:http://msdn.microsoft.com/zh-cn/library/fp142185.aspx



NPOI  无需安装microsoft excel  相关文章:http://www.cnblogs.com/atao/category/209358.html


不使用微软提供程序,使用NPOI导出Excel.cs

        /// <summary>
        /// 以指定的文件名,数据创建Excel模板,并返回文件路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string CreateDownloadTemplate(string fileName,DataTable dt)
        {
            string rootPath = HttpContext.Current.Server.MapPath(DOWNLOADTEMPLATE);
            string fullPath = rootPath.TrimEnd('\\') + "\\" + fileName.TrimStart('\\');
            string dir = Path.GetDirectoryName(fullPath);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            DataColumnCollection cols = dt.Columns;
            DataRowCollection rows = dt.Rows;
            int colCount = cols.Count;


            using (FileStream fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                HSSFWorkbook hssfworkbook = new HSSFWorkbook();
                ISheet sheet = hssfworkbook.CreateSheet("Sheet1");


                int n = -1;
                foreach (DataColumn col in cols)
                {
                    n++;
                    sheet.SetColumnWidth(n, 20 * 256);
                    if (n > 0)
                    {
                        ICell cell = sheet.GetRow(0).CreateCell(n);
                        cell.SetCellValue(col.Caption);
                    }
                    else
                    {
                        ICell cell = sheet.CreateRow(0).CreateCell(n);
                        cell.SetCellValue(col.Caption);
                    }
                }
                n = 0;
                foreach (DataRow row in rows)
                {
                    n++;
                    for (int i = 0; i < colCount; i++)
                    {
                        if (i > 0)
                        {
                            ICell cell = sheet.GetRow(n).CreateCell(i);
                            cell.SetCellValue(row[i].ToString());
                        }
                        else
                        {
                            ICell cell = sheet.CreateRow(n).CreateCell(i);
                            cell.SetCellValue(row[i].ToString());
                        }
                    }
                }


                hssfworkbook.Write(fs);
                fs.Close();
            }


            string fileUrl = "~/" + fullPath.Replace(HttpContext.Current.Server.MapPath("~"), "").Replace("\\", "/").TrimStart('/');
            fileUrl = VirtualPathUtility.MakeRelative("~/login.aspx", fileUrl);


            return fileUrl;
        }

导出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值