0

嘿,我已经完成了在 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;

            }
        }
    }
}                 
4

1 回答 1

0

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

DataTable dtTable = new DataTable();
OleDbConnection conn = new OleDbConnection(connstr);
dtTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
于 2012-07-31T07:32:04.033 回答