using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Data.OleDb;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] name = Directory.GetFiles(Server.MapPath("excel"));
foreach (string s in name)
{
//将文件名添加到ListBox中
ListBox1.Items.Add(Path.GetFileName(s));
}
//BindGv();
}
}
//绑定数据源
protected void BindGv()
{
GridView1.DataSource = GetDs();
GridView1.DataBind();
}
//获得数据源
protected DataSet GetDs()
{
// string excelPath = Server.MapPath("b.xls");
Session["txt"] = ListBox1.SelectedValue.ToString();
string path = Server.MapPath("~/excel/") + Session["txt"].ToString();
//初始化 FileInfo 类的实例,它作为文件路径的包装
//判断文件是否存在
string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + "; Extended Properties=Excel 8.0;";
OleDbConnection con = new OleDbConnection(conString);
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
//生成Excel的方法
private void Export(string FileName)
{
// Response.Charset = "GB2312";
// Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = "application / ms - excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
String path = Server.MapPath("~/excel/");
string excelPath = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
FileUpload1.PostedFile.SaveAs(path+ FileUpload1.FileName);
Response.Write("");
}
protected void Button2_Click(object sender, EventArgs e)
{
BindGv();
}
protected void Button3_Click(object sender, EventArgs e)
{
string[] name = Directory.GetFiles(Server.MapPath("excel"));
foreach (string s in name)
{
bool re = true;
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].ToString().Equals(Path.GetFileName(s)))
{
re = false;
}
}
if (re)
{
ListBox1.Items.Add(Path.GetFileName(s));
}
//将文件名添加到ListBox中
// ListBox1.Items.Add(Path.GetFileName(s));
}
}
}
C#的就是将excel读到textbox后再导入txt不会的再问我!
直接用下面的代码即可,不需要考虑txt文本有多少行数据,下面的方法会自动识别:
Sub readTxt()
Workbooks.OpenText Filename:="D:\1.txt", Space:=True
End Sub
相关参数:Filename指文件名,Space:=True表示以空格为分割符来载入数据,如果要指定分隔符可使用以下参数:
Tab以制表符为分隔符,Semicolon以分号为分隔符, Comma以逗号为分隔符, 若要自定义分隔符可设置Other参数为True,并在OtherChar参数指定分隔符(若分隔符为多个字符,则以第一个字符为准,其他字符忽略)
下面给出了一个以“/”为分隔符的例子:
Sub readTxt()
Workbooks.OpenText Filename:="D:\1.txt", Other:=True, OtherChar:="/"
End Sub
执行上述代码后,会将txt文件载入一个临时的excel文档里,程序不会 自动帮你保存,你需要自己手动保存这个临时文件。
如果你这个都要问,我还是劝你还是去看看书,
VB我不会用。很抱歉