public partial class _Default : System.Web.UI.Page
{
public string path3 = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ViewState.Add("addbtn","");
} if (this.ViewState["addbtn"].ToString() != "")
{
AddButton();
}
} private void AddButton()
{
Button btn = new Button();
btn.CommandArgument = "1";
btn.Command += new CommandEventHandler(Open_Click); btn.ID = "button1";
btn.Text = " 打开文件";
Panel1.Controls.Add(btn);
} protected void Open_Click(object sender, EventArgs e)
{
string dir = @"D:\AA\BB";
string inputpath1 = TextBox1.Text + TextBox2.Text + TextBox3.Text;
path3 = inputpath1;
string path = dir + "\\" + path3 + ".xls";
Excel.Application app;
Excel.Workbook workBook;
Excel.Worksheet workSheet;
app = new Excel.ApplicationClass();
app.Visible = true;
workBook = app.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
} protected void Button1_Click(object sender, EventArgs e)
{
Label label = new Label();
label.ID = "label4";
label.Text = "";
GridView gridview = new GridView();
gridview.ID = "GridView1";
Panel1.Controls.Add(label);
Panel1.Controls.Add(gridview); string inputpath = TextBox1.Text + TextBox2.Text + TextBox3.Text;
path3 = inputpath;
string dir = @"D:\AA\BB";
string[] files1 = Directory.GetFiles(dir); foreach (string i in files1)
{
string path1 = dir + "\\" + inputpath + ".xls";
if (path1 == i)
{ this.ViewState["addbtn"] = "1";
AddButton(); label.Text = path1 ;
string excelFilePath = path1;
Excel.Application myExcel = new Excel.ApplicationClass();
object oMissing = System.Reflection.Missing.Value;
myExcel.Application.Workbooks.Open(excelFilePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
Excel.Workbook myBook = myExcel.Workbooks[1];
Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1]; DataTable dt = new DataTable("mytable"); dt.Columns.Add("F1", System.Type.GetType("System.String")); dt.Columns.Add("F2", System.Type.GetType("System.String")); dt.Columns.Add("F3", System.Type.GetType("System.String"));
dt.Columns.Add("F4", System.Type.GetType("System.String"));
dt.Columns.Add("F5", System.Type.GetType("System.String"));
DataSet myDs = new DataSet();
myDs.Tables.Add(dt);
DataRow myRow;
myDs.Clear();
for (int j = 1; j <= 12; j++)
{
myRow = myDs.Tables["mytable"].NewRow();
for (int k = 1; k <= 5; k++)
{
Excel.Range r = (Excel.Range)mySheet.Cells[j, k];
string strValue = r.Text.ToString();
string aa = strValue;
string columnname = "F" + k.ToString();
myRow[columnname] = strValue;
}
myDs.Tables["mytable"].Rows.Add(myRow);
} GridView1.DataSource = myDs.Tables["mytable"].DefaultView;
GridView1.DataBind();
myExcel.Application.Workbooks.Close();
myExcel.Quit();
myExcel = null; if (!IsPostBack)
{
Response.ClearHeaders();
Response.Redirect(path1);
} break;
}
else
{
label.Text = "文件不存在";
GridView1.DataSource = null;
GridView1.DataBind();
}
}
}
}
现在的问题是,找到文件,能出现打开文件按钮,找不到文件,就不出现,但是,点击打开文件按钮会报错:
能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。源错误:
行 71: GridView gridview = new GridView();
行 72: gridview.ID = "GridView1";
行 73: Panel1.Controls.Add(label);
行 74: Panel1.Controls.Add(gridview);
行 75:
源文件: d:\示例代码\COO\WebSite21\Default.aspx.cs 行: 73
头疼啊,哪位给我指点下
{
public string path3 = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ViewState.Add("addbtn","");
} if (this.ViewState["addbtn"].ToString() != "")
{
AddButton();
}
} private void AddButton()
{
Button btn = new Button();
btn.CommandArgument = "1";
btn.Command += new CommandEventHandler(Open_Click); btn.ID = "button1";
btn.Text = " 打开文件";
Panel1.Controls.Add(btn);
} protected void Open_Click(object sender, EventArgs e)
{
string dir = @"D:\AA\BB";
string inputpath1 = TextBox1.Text + TextBox2.Text + TextBox3.Text;
path3 = inputpath1;
string path = dir + "\\" + path3 + ".xls";
Excel.Application app;
Excel.Workbook workBook;
Excel.Worksheet workSheet;
app = new Excel.ApplicationClass();
app.Visible = true;
workBook = app.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);
} protected void Button1_Click(object sender, EventArgs e)
{
Label label = new Label();
label.ID = "label4";
label.Text = "";
GridView gridview = new GridView();
gridview.ID = "GridView1";
Panel1.Controls.Add(label);
Panel1.Controls.Add(gridview); string inputpath = TextBox1.Text + TextBox2.Text + TextBox3.Text;
path3 = inputpath;
string dir = @"D:\AA\BB";
string[] files1 = Directory.GetFiles(dir); foreach (string i in files1)
{
string path1 = dir + "\\" + inputpath + ".xls";
if (path1 == i)
{ this.ViewState["addbtn"] = "1";
AddButton(); label.Text = path1 ;
string excelFilePath = path1;
Excel.Application myExcel = new Excel.ApplicationClass();
object oMissing = System.Reflection.Missing.Value;
myExcel.Application.Workbooks.Open(excelFilePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
Excel.Workbook myBook = myExcel.Workbooks[1];
Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1]; DataTable dt = new DataTable("mytable"); dt.Columns.Add("F1", System.Type.GetType("System.String")); dt.Columns.Add("F2", System.Type.GetType("System.String")); dt.Columns.Add("F3", System.Type.GetType("System.String"));
dt.Columns.Add("F4", System.Type.GetType("System.String"));
dt.Columns.Add("F5", System.Type.GetType("System.String"));
DataSet myDs = new DataSet();
myDs.Tables.Add(dt);
DataRow myRow;
myDs.Clear();
for (int j = 1; j <= 12; j++)
{
myRow = myDs.Tables["mytable"].NewRow();
for (int k = 1; k <= 5; k++)
{
Excel.Range r = (Excel.Range)mySheet.Cells[j, k];
string strValue = r.Text.ToString();
string aa = strValue;
string columnname = "F" + k.ToString();
myRow[columnname] = strValue;
}
myDs.Tables["mytable"].Rows.Add(myRow);
} GridView1.DataSource = myDs.Tables["mytable"].DefaultView;
GridView1.DataBind();
myExcel.Application.Workbooks.Close();
myExcel.Quit();
myExcel = null; if (!IsPostBack)
{
Response.ClearHeaders();
Response.Redirect(path1);
} break;
}
else
{
label.Text = "文件不存在";
GridView1.DataSource = null;
GridView1.DataBind();
}
}
}
}
现在的问题是,找到文件,能出现打开文件按钮,找不到文件,就不出现,但是,点击打开文件按钮会报错:
能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。源错误:
行 71: GridView gridview = new GridView();
行 72: gridview.ID = "GridView1";
行 73: Panel1.Controls.Add(label);
行 74: Panel1.Controls.Add(gridview);
行 75:
源文件: d:\示例代码\COO\WebSite21\Default.aspx.cs 行: 73
头疼啊,哪位给我指点下
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货