如何在asp.net中访问excel的所有属性,如工作表名称,字体,颜色,粗体,斜体等

时间:2012-07-31 06:44:27

标签: asp.net .net xml excel

嘿,我已经完成了上传并在asp.net中显示excel文件的编码 但问题是:

1)如何知道excel文件的工作表名称(我将sheet1作为默认工作表,但如果用户试图上传具有不同工作表名称的excel文件,则显示错误

2)如何知道excel文件的属性,如excel文件的标题,其中数据的颜色是什么大小和字体?因此,我显示的输出与excel文件中的输出相同

以下是我的代码

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

namespace ExcelToAsp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnUpload_Click(object sender, EventArgs e)
        {
            if ((TxtFilePath.HasFile))
            {

                OleDbConnection conn = new OleDbConnection();
                OleDbCommand cmd = new OleDbCommand();
                OleDbDataAdapter da = new OleDbDataAdapter();
                DataSet ds = new DataSet();
                string query = null;
                string connString = "";
                string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
                string strFileType =
                System.IO.Path.GetExtension(TxtFilePath.FileName).ToString().ToLower();
                //Check file type
                if (strFileType == ".xls" || strFileType == ".xlsx")
                    TxtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
                else
                {
                    LblMsg.Text = "Only excel files allowed";
                    LblMsg.ForeColor = System.Drawing.Color.Red;
                    LblMsg.Visible = true;
                    return;
                }
                string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);

                //Connection String to Excel Workbook
                if (strFileType.Trim() == ".xls")
                {
                    connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                       strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                }
                else if (strFileType.Trim() == ".xlsx")
                {
                    connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                }

                query = "SELECT * FROM [Sheet1$]";//query = "SELECT [Country],[Capital] FROM [Sheet1$] WHERE[Currency]=’Rupee’"//query = "SELECT [Country],[Capital] FROM [Sheet1$]"

                //Create the connection object
                conn = new OleDbConnection(connString);
                //Open connection
                if (conn.State == ConnectionState.Closed) conn.Open();
                //Create the command object
                cmd = new OleDbCommand(query, conn);
                da = new OleDbDataAdapter(cmd);
                ds = new DataSet();
                da.Fill(ds);

                GrvExcelData.DataSource = ds.Tables[0];
                GrvExcelData.DataBind();
                LblMsg.Text = "Data retrieved successfully! Total Records:" + ds.Tables[0].Rows.Count;
                LblMsg.ForeColor = System.Drawing.Color.Green;
                LblMsg.Visible = true;
                da.Dispose();
                conn.Close();
                conn.Dispose();
            }
            else
            {
                LblMsg.Text = "Please select an excel file first";
                LblMsg.ForeColor = System.Drawing.Color.Red;
                LblMsg.Visible = true;

            }
        }
    }
}                 

1 个答案:

答案 0 :(得分:0)

您可以使用oledb获取工作表名称。

DataTable dtTable = new DataTable();
OleDbConnection conn = new OleDbConnection(connstr);
dtTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);