读书人

用c# 将txt变换为excel

发布时间: 2013-07-08 14:13:00 作者: rapoo

用c# 将txt转换为excel
我的txt如下:
39.4% 2013-07-03 15-38-59
35.8 2013-07-03 15-38-59

39.5% 2013-07-03 15-39-03
35.8 2013-07-03 15-39-03

39.7% 2013-07-03 15-39-06
35.7 2013-07-03 15-39-06

39.8% 2013-07-03 15-39-10
35.6 2013-07-03 15-39-10

......
想分为四列
39.4% 2013-07-03 15-38-59 35.8 2013-07-03 15-38-59
39.5% 2013-07-03 15-39-03 35.8 2013-07-03 15-39-03
39.7% 2013-07-03 15-39-06 35.7 2013-07-03 15-39-06
39.8% 2013-07-03 15-39-10 35.6 2013-07-03 15-39-10
........

请问该怎么做?最好用代码教我与给出具体实现。网上的小工具转化都做不到这种程度。 C# Excel txt
[解决办法]
就上转换格式这一部分的代码,生成到excel,自己百度下或者参考我的博客:
生成excel

 string result = string.Empty;
StreamReader sr = new StreamReader("test.txt");
int line = 0;
while (sr.Peek() > 0)
{
line++;
result += sr.ReadLine() + " ";
if (line % 2 == 0)
{
result += "\r\n";
}
}

Console.WriteLine(result);


[解决办法]
给你一个不调用excel组件生成excel表的代码,我是用VB写的,你参考下


Private Function ToExcelFile(ByVal table As DataTable, ByVal excelFile As String, ByVal tableName As String) As Boolean
Try
Dim Connection As New OleDbConnection(String.Format("Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source = {0};Extended Properties=Excel 8.0", excelFile))
Dim MyDataAdapter = New OleDbDataAdapter("select * from " & tableName, Connection)
Dim m_CommandBuilder As New OleDbCommandBuilder(MyDataAdapter)
Dim MyDataSet As New DataSet()
Dim objCmd As New OleDbCommand
Dim ColumnList As New List(Of String)
Dim Columns As String
Connection.Open()
For Each col As DataColumn In Table.Columns
ColumnList.Add(col.ColumnName & " varchar")
Next
Columns = "Create TABLE " & tableName & "(" & String.Join(",", ColumnList.ToArray) & ")"
objCmd = New OleDbCommand(Columns, Connection)
objCmd.ExecuteNonQuery()
MyDataAdapter.Fill(MyDataSet, "Temp")


For i As Integer = 0 To table.Rows.Count - 1
MyDataSet.Tables("Temp").Rows.Add(table.Rows(i).ItemArray)
Next
MyDataAdapter.Update(MyDataSet, "Temp")
MyDataAdapter.Dispose()
MyDataSet.Dispose()
Connection.Close()
Catch ex As Exception
Return False
End Try
Return True
End Function


[解决办法]
搜索一下读txt文件的 变成string
然后分割 整理成你要的数据List<string>

接下来
http://tonyqus.sinaapp.com/tutorial
[解决办法]

string result = string.Empty;
StreamReader sr = new StreamReader("C:\\1.txt");
List<ExcelTable> tablelist = new List<ExcelTable>();
ExcelTable model = null;
int line = 0;
while (sr.Peek() > 0)
{
string readlinestr = sr.ReadLine().Trim();
if (String.IsNullOrEmpty(readlinestr))
{
continue;
}
line++;


result += readlinestr + " ";

if (line % 2 == 0)
{
foreach (Match m in Regex.Matches(result, @"(?<percent>\d+\.\d+%)\s*(?<date1>\d{4}-\d{1,2}-\d{1,2}\s*\d{1,2}-\d{1,2}-\d{1,2})\s*(?<num>\d+\.\d+)\s*(?<date2>\d{4}-\d{1,2}-\d{1,2}\s*\d{1,2}-\d{1,2}-\d{1,2})"))
{
model = new ExcelTable();
model.field1 = m.Groups["percent"].Value;
model.field2 = m.Groups["date1"].Value;
model.field3 = m.Groups["num"].Value;
model.field4 = m.Groups["date2"].Value;
tablelist.Add(model);
}
result += "\r\n";
}
}

// Console.WriteLine(result);

foreach (ExcelTable m in tablelist)
{
Console.WriteLine(m.field1 + "\t" + m.field2 + "\t" + m.field3 + "\t" + m.field4);


}




写入excel自己网上找

读书人网 >C#

热点推荐