我要把txt转为Excel的表头对应的改成中文显示,并动态的根据city列,生成表名(就是说不是固定的列标)。怎么处理?

解决方案 »

  1.   

    我是要用Winform来做成一个小工具。用以将文本文件转换成Excel的。大家来来帮帮忙啊!
      

  2.   

    多查查VBA方面的资料,C#操作txt文件和excel文件都不是非常难的啊,具体怎么做就参考下类似 怎么用Excel.Application生成excel文件(http://topic.csdn.net/t/20030916/15/2265940.html)这样的文章啊
      

  3.   

    嗯。 已经完成的差不多了只是差这些地方要改变。没有用Excel的dll
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Data;
    using System.Data.OleDb;namespace ReadSeparatedValuesTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                /*
                 * 1. Issuse
                 * Let's say I have 4 fields delimited by tab in each line in the tab-delimited.txt file. 
                 * I also have a schema.ini file in the same folder to indicate the format of the tab-delimited.txt file.
                 * But it seems the schema.ini file does not override the setting in the system registry(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text\Format). 
                 * The text engines can not recognize tab as the delimiter. So it still reads the four fields together into one field.
                 * 
                 * 2. Solution But Tedious
                 * Config the default format from CSVDelimited to TabDelimited in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text\Format, you will get the expected result.
                 */
                string dataSourcePath = Environment.CurrentDirectory + "\\";
                string dataFileName = "tab-delimited.txt";
                string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dataSourcePath + @";Extended Properties=""text;HDR=NO;FMT=TabDelimited""";
                OleDbConnection conn = new OleDbConnection(connString);
                OleDbCommand cmd = conn.CreateCommand();
                cmd.CommandText = String.Format("SELECT * FROM [{0}]", dataFileName);
                DataTable dt = new DataTable(Path.GetFileNameWithoutExtension(dataFileName));
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(dt);
                // output as xml.
                dt.WriteXml(dataFileName + ".xml");
                // output into current console.
                Console.WriteLine("The imported data has {0} column(s):", dt.Columns.Count);            
                foreach (DataColumn col in dt.Columns)
                {
                    Console.Write(col.ColumnName);
                    Console.Write("\t");
                }
                Console.WriteLine();            foreach (DataRow row in dt.Rows)
                {
                    foreach (DataColumn col in dt.Columns)
                    {
                        Console.Write(row[col]);
                        Console.Write("\t");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
                //
                Console.Read();
            }
        }
    }将文本文件读到DataTable里面,然后导出到excel
      

  5.   

    在tab-delimited.txt所在位置新建一个schema.ini文件[tab-delimited.txt]
    Format=TabDelimited
      

  6.   

    给lz另一条路,先直接把你的txt文件改名成xls,再用excel打开看看(看lz给出的例子,应该是直接可以被当成xls打开的)
    然后剩下的工作就是换掉标题,和按照地区分多个文件了,这样的纯文本操作应该不难吧
      

  7.   

    用流来读取text文本,然后用代码来控制分类,可以保存为后缀名为.xsl格式的,双击excel还是可以打开的。
      

  8.   

     是的.我现在就是没有办法按照city这个列的值来分别命名Excel.因为city的下标不是固定的.有什么好的办法么?