using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.OleDb;public partial class _Default : System.Web.UI.Page
{
public string path3 = "";
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(gridview);
Panel1.Controls.Add(label); 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)
{
label.Text = path1;
Button button = new Button();
button.ID = "botton1";
button.Text = "打开文件";
Panel1.Controls.Add(button);
button.Click += new EventHandler(OpenClick);
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();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
} public void OpenClick(object sender, EventArgs e)
{ string dir = @"D:\AA\BB";
string inputpath = TextBox1.Text + TextBox2.Text + TextBox3.Text;
path3 = inputpath; 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); }
}这是源代码,不好意思,新手,有点乱。
我的问题就是:生成的那个“打开文件”的按钮,虽然网页里面生成了,但是用鼠标一点,就没了,然后我把它放到页面加载的方法里面,虽然好用,但是找不到文件的时候,这个按钮也存在。
想请高手解决下,怎么弄?
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.OleDb;public partial class _Default : System.Web.UI.Page
{
public string path3 = "";
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(gridview);
Panel1.Controls.Add(label); 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)
{
label.Text = path1;
Button button = new Button();
button.ID = "botton1";
button.Text = "打开文件";
Panel1.Controls.Add(button);
button.Click += new EventHandler(OpenClick);
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();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
} public void OpenClick(object sender, EventArgs e)
{ string dir = @"D:\AA\BB";
string inputpath = TextBox1.Text + TextBox2.Text + TextBox3.Text;
path3 = inputpath; 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); }
}这是源代码,不好意思,新手,有点乱。
我的问题就是:生成的那个“打开文件”的按钮,虽然网页里面生成了,但是用鼠标一点,就没了,然后我把它放到页面加载的方法里面,虽然好用,但是找不到文件的时候,这个按钮也存在。
想请高手解决下,怎么弄?
解决方案 »
- C#中如何获取一个键盘事件
- C# 中类似VB中 With end With 的语句是什么?
- ArcEngine编程中过滤器类在哪里有?
- Remoting的TCP通道是用SOCKET通信?
- 奇怪,为何我一执行INSERT数据库就变为制度模式了,不让插入数据阿?各位帮帮我~
- 关于SQL存储过程的怪问题,请大家帮忙!
- vs2005如何开始一个使用c#得asp.net webform项目,我安装vs2005得时候只是选择了c#?
- 列名 'xxx' 无效。
- 為什麼執行 "private Word.ApplicationClass WordApp = new Word.ApplicationClass();" 出問題
- 问一下,大家都用什么编辑器写C#程序,谁有比较好的这样的编辑软件?
- webBrowserBody.Url 不能赋值(不能跳转到其他页面)
- 字符串转换日期类型的问题
根据文件的是否存在控制是否重新生成按钮
if(文件存在)
{
Button b = new Button();
}
Page_Load(..){
if(this.ViewState["aaa"] == null)
{
this.ViewState.Add("aaa","");
}
if(this.ViewState["aaa"].ToString() == "1")
{
//new button .....
}
}
Button1_Click(..)
{
this.ViewState["aaa"] = "";//不显示
this.ViewState["aaa"] = "1";//显示
}