Aspose.Cells.dll的运用

本文介绍了一种通过C#实现的Excel表格操作方法,包括读取表名、创建新工作表及导出数据的具体步骤。提供了从数据库读取数据并填充到Excel的方法,还涉及了如何设置样式以及导出Excel文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取表格:

  /// <summary>
        /// 读取Excel表名
        /// </summary>
        /// <param name="Path">Excel路径</param>
        /// <returns>表名数据集合</returns>
        public static DataTable GetExcelTableName(string Path)
        {
            DataTable _Table = new DataTable();
            try
            {
                if (System.IO.File.Exists(Path))
                {
                    OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.ace.oledb.12.0;Extended Properties=\"Excel 12.0\";Data Source=" + Path);
                    _ExcelConn.Open();
                    _Table = _ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    _ExcelConn.Close();
                }
                return _Table;
            }
            catch
            {
                return _Table;
            }

        }



web方式的导出:

       string name = DateTime.Now.ToString("yyyyMMddHHmmss") + Session["uid"].ToString();//表单的名称

        Workbook wb = new Workbook();
        wb.Worksheets.Clear();
        wb.Worksheets.Add("海关申报报表 - " + datetxt.Value);//New Worksheet1是Worksheet的name即 表明
        Worksheet ws = wb.Worksheets[0];

        //1-12 列合并  写标题
        ws.Cells.Merge(0, 0, 1, 11);
        ws.Cells[0, 0].PutValue("海关申请报表 - " + DateTime.Parse(datetxt.Value).ToLongDateString());

//创建列名
        ws.Cells[1, 0].PutValue("单号");

        ws.Cells[1, 1].PutValue("袋号");

        ws.Cells[1, 2].PutValue("寄件人");
        ws.Cells[1, 3].PutValue("寄件人电话");
        ws.Cells[1, 4].PutValue("寄件人地址");

        ws.Cells[1, 5].PutValue("收件人");
        ws.Cells[1, 6].PutValue("收件人电话");

        ws.Cells[1, 7].PutValue("收件人地址");
        ws.Cells[1, 8].PutValue("品名");

        ws.Cells[1, 9].PutValue("重量");
        ws.Cells[1, 10].PutValue("航空号");

//循环表读取数据,并填充
        for (int i = 0; i < dt.Rows.Count; i++)
        {


            ws.Cells[i + 2, 0].PutValue(dt.Rows[i]["TrackingNo"]);
            ws.Cells[i + 2, 1].PutValue(dt.Rows[i]["Description"]);
            ws.Cells[i + 2, 2].PutValue(dt.Rows[i]["Sender"]);
            ws.Cells[i + 2, 3].PutValue(dt.Rows[i]["FromPhoneNo"]);
            ws.Cells[i + 2, 4].PutValue(dt.Rows[i]["FromAddress"]);

            ws.Cells[i + 2, 5].PutValue(dt.Rows[i]["Receiver"]);
            ws.Cells[i + 2, 6].PutValue(dt.Rows[i]["ToPhoneNo"]);

            ws.Cells[i + 2, 7].PutValue(dt.Rows[i]["ToAddress"]);
            string pname = "";
            GetNameAndWeight(dt.Rows[i]["TrackingNo"].ToString(), ref pname);
            ws.Cells[i + 2, 8].PutValue(pname);

            ws.Cells[i + 2, 9].PutValue(dt.Rows[i]["ChargedWt"]);
            ws.Cells[i + 2, 10].PutValue(dt.Rows[i]["AerialNo"]);
        }

        string path = ApplicationInfo.PhysicalApplicationPath + "Temp//" + name + ".xls";//保存的路径

        #region 样式开始 在      wb.Save(path); 之前使用

        Aspose.Cells.Style st = wb.DefaultStyle;
        st.Borders.SetStyle(CellBorderType.Thin);
        st.Borders.DiagonalStyle = CellBorderType.None;
        StyleFlag stg = new StyleFlag();
        stg.All = true;
        ws.Cells.ApplyStyle(st, stg);

        #endregion 样式结束
        wb.Save(path);
        Dc(path, name, "海关申报报表" + DateTime.Parse(datetxt.Value).ToLongDateString());


    private void Dc(string path, string name, string cname)
    {
        FileInfo fi = new FileInfo(path);//excelFile为文件在服务器上的地址
        HttpResponse contextResponse = HttpContext.Current.Response;
        contextResponse.Clear();
        contextResponse.Buffer = true;
        contextResponse.Charset = "utf-8"; //设置了类型为中文防止乱码的出现 
        string filename = HttpUtility.UrlEncode(cname + ".xls");
        contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", filename)); //定义输出文件和文件名 
        contextResponse.AppendHeader("Content-Length", fi.Length.ToString());
        contextResponse.ContentEncoding = Encoding.Default;
        contextResponse.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。

        contextResponse.WriteFile(fi.FullName);
        contextResponse.Flush();
        contextResponse.End();
    }


WF中直接去掉 Dc(path, name, "海关申报报表" + DateTime.Parse(datetxt.Value).ToLongDateString());方法即可。

如需要弹出客户自己选择路径,则自己加一个弹出控件即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值