读取txt文本中的值 用gridview显示
来源:互联网 发布:mac关闭safari 编辑:程序博客网 时间:2024/04/28 03:37
public DataTable LoadData(string fileName)
{
DataTable dt = new DataTable();
dt.Columns.Add("抽奖用户", typeof(string));
dt.Columns.Add("抽奖时间", typeof(string));
dt.Columns.Add("是否中奖", typeof(string));
dt.Columns.Add("剩余抽奖次数", typeof(string));
string path = Server.MapPath(fileName);
FileUpload1.SaveAs(path);
//打开文件,将文本读入流中//使用系统默认编码试一下StreamReader reader=new StreamReader(filename,System.Text.Encoding.Default)//将流中的文本输出到textBoxMessage中TextBoxMessage.Text=reader.ReadToEnd();//关闭流reader.Close();
StreamReader reader = new StreamReader(path, System.Text.Encoding.Default);
this.Label1.Text = reader.ReadToEnd();//关闭流reader.Close();
string[] arrstr = this.Label1.Text.Split(new char[] { '\r', '\n' });//截取行
string[] cells;
for (int i = 0; i < arrstr.Length; i++)//循环行数
{
cells = arrstr[i].ToString().Split('\t');//取每行中的每一列
DataRow rows = dt.NewRow();
rows["抽奖用户"] = cells[0];
rows["抽奖时间"] = cells[1];
rows["是否中奖"] = cells[2];
rows["剩余抽奖次数"] = cells[3];
dt.Rows.Add(rows);
}
reader.Close();
return dt;
}
protected void check_Click(object sender, EventArgs e)
{
string filename = this.FileUpload1.FileName.Trim();
if (!filename.EndsWith("txt"))
{
ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('只能传txt文件')", true);
return;
}
this.GridView1.DataSource= LoadData(filename);
this.GridView1.DataBind();
}
2.前台
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button runat="server" ID="check" Text="导入" onclick="check_Click"/>
<asp:Label ID="Label1" runat="server" Text="Label" Visible="false"></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="抽奖用户" HeaderText="抽奖用户" />
<asp:BoundField DataField="抽奖时间" HeaderText="抽奖时间" />
<asp:BoundField DataField="是否中奖" HeaderText="是否中奖" />
<asp:BoundField DataField="剩余抽奖次数" HeaderText="剩余抽奖次数" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
3.txt文本中的内容:
抽奖用户 抽奖时间 是否中奖 剩余抽奖次数
bowencyanj 2011-04-20 14:10:38 是 0
bowencyanj 2011-04-20 14:10:10 是 1
bowencyanj 2011-04-20 14:08:52 否 2
bowencyanj 2011-04-20 11:06:11 否 3
bowencyanj 2011-04-20 11:06:02 否 4
bowencyanj 2011-04-20 11:05:34 是 5
bowencyanj 2011-04-20 10:01:58 是 6
bowencyanj 2011-04-20 10:00:46 否 7
bowencyanj 2011-04-20 09:59:12 是 8
bowencyanj 2011-04-20 09:57:25 是 9
- 读取txt文本中的值 用gridview显示
- 读取txt文件中的文本
- QT读取txt文本,并且显示
- 使用StreamReader 读取txt文本中的信息
- C++读取txt文本中的矩阵数据
- 获取GRIDVIEW中的TemplateField显示的文本值
- 2.读取TXT文本内容,显示至图形界面中
- 使用 PHP 读取文本(TXT)文件 并分页显示
- Unity读取txt文本中文不显示的问题
- java读取txt文本
- 读取txt文本
- txt文本读取删除
- C++ 读取txt文本
- 数据库连接函数并读取TXT文本中的数据
- ajax读取txt文本内容
- ajax读取txt文本内容
- 1.读取TXT文本内容
- 按行读取TXT文本
- Fisher线性分类器(2)--- 各最优参数的确定
- 在Linux下产生并调试core文件
- 理解.NET程序集的执行过程
- 浅析C语言的一个关键字——register
- Linux下cronolog安装配置
- 读取txt文本中的值 用gridview显示
- Linux 远程copy
- Oracle数据库分区表操作方法
- C#生成XML的三种途径
- SQL 语句
- Windows下编译sqlite3生成动态链接库并使用
- [Hook]个人学习笔记——挂个简单的钩子(全局鼠标Hook)(.NET)
- 《Object-Oriented Programming With ANSI-C》之第四章(继承——代码重用和改进)
- lR关联功能总结