将一个文件夹下面的所有txt文档中的内容自动导出到一个EXCEL中,这个怎么做呢?
麻烦大家给点思路呗,小女新手多谢指教!

解决方案 »

  1.   

    遍历文件夹所有的txt文件,然后导入~~~~~~~~~~关键是不知道你怎么导入?
      

  2.   

    把文本看做字节流,写入到Excel中
      

  3.   

    txt导入数据库最麻烦了,如果是xml文件就好办了。我建议你这样构建你的项目:
    1,使用xml存储文件
    2,使用OpenFileDialog()选择要导入的xml文件(不建议遍历文件夹)
    3,连接xml读入DataSet,直接写入excel即可
      

  4.   

    我的这些txt文档是用手机测量出来的数据,然后从手机考到电脑上,再将这些文档转换成一个excel,一个txt转成一个excel我会了,可是把这一对都一起转过来还得是不能更改的、还得一起的,不懂
      

  5.   

    txt导入excel的话,如果不分割的话只能导入excel的A1单元格~~~~~~~你既然单个会导入excel,那么加个for循环即可
    for(int i=0;i<文件名数组.Count;i++)
    {
    循环导入就行了
    }
      

  6.   

    文档里就有数据没有说明,要求是导出来的excel要有列标识,这个可以循环么,不怎么明白
      

  7.   


    所以说你就不能遍历文件夹来导入excel了~~~~~~~不太明白你的txt文件具体的格式和你操作的意义,所以你能将你的数据导出为xml文件而非txt文件,再进行操作就方便的多了
      

  8.   

    不需要程序.开始菜单=>运行=>输入:
    cmd /c for %1 in ("TXT所在文件夹\*.txt") do excel %1
      

  9.   

    你应该把你的TXT格式发出来,如果txt里没有“,”,空格或其他的分隔符,那根据什么导入到excel呢?真像7楼那样都导入到A1单元格,那就没什么意义了.
      

  10.   

    每个txt里数据都不一样啊,这里怎么发图片啊?
      

  11.   

    一个a.txt里面数据是这样的
    HG.
    Std.Curve
    Peak Area
    ng/ml
    ng/ml
    0
    10
    0
    10
    1
    然后b.txt里面数据是这样的
    1 Blank1 80.055 80.055 0 ng/ml 0 11-3-8 16:32:51
    2 Blank2 79.984 79.984 0 ng/ml 0 11-3-8 16:33:04
    3 Blank3 80.066 80.066 0 ng/ml 0 11-3-8 16:33:16
    4 Blank4 80.008 80.008 0 ng/ml 0 11-3-8 16:33:27
    5 Blank5 79.964 79.964 0 ng/ml 0 11-3-8 16:33:42
    6 Blank6 80.019 80.019 0 ng/ml 0 11-3-8 16:34:01
    7 Blank7 79.909 79.909 0 ng/ml 0 11-3-8 16:34:23
    8 Blank8 79.830 79.830 0 ng/ml 0 11-3-8 16:34:35
    9 Blank9 79.891 79.891 0 ng/ml 0 11-3-8 16:34:48
    10 Blank10 79.838 79.838 0 ng/ml 0 11-3-8 16:35:02
    11 Blank11 79.737 79.737 0 ng/ml 0 11-3-8 16:35:14
    12 STD1 79.936 -0.085 10 ng/ml 0 11-3-8 16:35:27
    13 STD2 79.936 -0.194 20 ng/ml -28.78924 11-3-8 16:35:38
    然后导出来之后每列要添加一个名称
      

  12.   

    这就太麻烦了,最好还是将xml导入excel
      

  13.   

    先给你一个写CSV的类using System;
    using System.Text;
    using System.Collections;
    using System.IO;
    using System.Data;namespace CSVutil
    {
        #region 类说明信息
        /// <summary>
        ///  <DL>
        ///  <DT><b>写CSV文件类,首先给CSV文件赋值,最后通过Save方法进行保存操作</b></DT>
        ///   <DD>
        ///    <UL> 
        ///    </UL>
        ///   </DD>
        ///  </DL>
        ///  <Author>Tim Zhang</Author>    
        ///  <CreateDate>2011.03.03</CreateDate>
        ///  <Company>B.I.W</Company>
        ///  <Version>1.0</Version>
        /// </summary>
        #endregion
        public class CsvStreamWriter
        {
            private ArrayList rowAL;        //行链表,CSV文件的每一行就是一个链
            private string fileName;       //文件名
            private string filePath;
            private Encoding encoding;       //编码        public CsvStreamWriter()
            {
                this.rowAL = new ArrayList();
                this.fileName = "";
                this.encoding = Encoding.Default;
            }        /// <summary>
            /// 
            /// </summary>
            /// <param name="fileName">文件名,包括文件路径</param>
            public CsvStreamWriter(string fileName)
            {
                this.rowAL = new ArrayList();
                this.fileName = fileName;
                this.encoding = Encoding.Default;
            }        /// <summary>
            /// 
            /// </summary>
            /// <param name="fileName">文件名,包括文件路径</param>
            /// <param name="encoding">文件编码</param>
            public CsvStreamWriter(string fileName, Encoding encoding)
            {
                this.rowAL = new ArrayList();
                this.fileName = fileName;
                this.encoding = encoding;
            }        /// <summary>
            /// row:行,row = 1代表第一行
            /// col:列,col = 1代表第一列
            /// </summary>
            public string this[int row, int col]
            {
                set
                {
                    //对行进行判断
                    if (row <= 0)
                    {
                        throw new Exception("行数不能小于0");
                    }
                    else if (row > this.rowAL.Count) //如果当前列链的行数不够,要补齐
                    {
                        for (int i = this.rowAL.Count + 1; i <= row; i++)
                        {
                            this.rowAL.Add(new ArrayList());
                        }
                    }
                    else
                    {
                    }
                    //对列进行判断
                    if (col <= 0)
                    {
                        throw new Exception("列数不能小于0");
                    }
                    else
                    {
                        ArrayList colTempAL = (ArrayList)this.rowAL[row - 1];                    //扩大长度
                        if (col > colTempAL.Count)
                        {
                            for (int i = colTempAL.Count; i <= col; i++)
                            {
                                colTempAL.Add("");
                            }
                        }
                        this.rowAL[row - 1] = colTempAL;
                    }
                    //赋值
                    ArrayList colAL = (ArrayList)this.rowAL[row - 1];                colAL[col - 1] = value;
                    this.rowAL[row - 1] = colAL;
                }
            }
            /// <summary>
            /// 文件名,包括文件路径
            /// </summary>
            public string FileName
            {
                set
                {
                    this.fileName = value;
                    int nIndex = fileName.LastIndexOf(@"\");
                    this.filePath = fileName.Substring(0, nIndex);
                }
            }        /// <summary>
            /// 文件编码
            /// </summary>        public Encoding FileEncoding
            {
                set
                {
                    this.encoding = value;
                }
            }        /// <summary>
            /// 获取当前最大行
            /// </summary>
            public int CurMaxRow
            {
                get
                {
                    return this.rowAL.Count;
                }
            }        /// <summary>
            /// 获取最大列
            /// </summary>
            public int CurMaxCol
            {
                get
                {
                    int maxCol;                maxCol = 0;
                    for (int i = 0; i < this.rowAL.Count; i++)
                    {
                        ArrayList colAL = (ArrayList)this.rowAL[i];                    maxCol = (maxCol > colAL.Count) ? maxCol : colAL.Count;
                    }                return maxCol;
                }
            }        /// <summary>
            /// 添加表数据到CSV文件中
            /// </summary>
            /// <param name="dataDT">表数据</param>
            /// <param name="beginCol">从第几列开始,beginCol = 1代表第一列</param>
            public void AddData(DataTable dataDT, int beginCol)
            {
                if (dataDT == null)
                {
                    throw new Exception("需要添加的表数据为空");
                }
                int curMaxRow;            curMaxRow = this.rowAL.Count;
                for (int i = 0; i < dataDT.Rows.Count; i++)
                {
                    for (int j = 0; j < dataDT.Columns.Count; j++)
                    {
                        this[curMaxRow + i + 1, beginCol + j] = dataDT.Rows[i][j].ToString();
                    }
                }
            }        /// <summary>
            /// 保存数据,如果当前硬盘中已经存在文件名一样的文件,将会覆盖
            /// </summary>
            public void Save()
            {
                //对数据的有效性进行判断
                if (this.fileName == null)
                {
                    throw new Exception("缺少文件名");
                }
                if (!Directory.Exists(filePath))//如果文件夹不存在
                {
                    Directory.CreateDirectory(filePath);//则创建文件夹
                }
                if (File.Exists(this.fileName))
                {
                    File.Delete(this.fileName);
                }
                if (this.encoding == null)
                {
                    this.encoding = Encoding.Default;
                }
                System.IO.StreamWriter sw = new StreamWriter(this.fileName, false, this.encoding);            for (int i = 0; i < this.rowAL.Count; i++)
                {
                    sw.WriteLine(ConvertToSaveLine((ArrayList)this.rowAL[i]));
                }            sw.Close();
            }        /// <summary>
            /// 保存数据,如果当前硬盘中已经存在文件名一样的文件,将会覆盖
            /// </summary>
            /// <param name="fileName">文件名,包括文件路径</param>
            public void Save(string fileName)
            {
                this.fileName = fileName;
                Save();
            }        /// <summary>
            /// 保存数据,如果当前硬盘中已经存在文件名一样的文件,将会覆盖
            /// </summary>
            /// <param name="fileName">文件名,包括文件路径</param>
            /// <param name="encoding">文件编码</param>
            public void Save(string fileName, Encoding encoding)
            {
                this.fileName = fileName;
                this.encoding = encoding;
                Save();
            }
            /// <summary>
            /// 转换成保存行
            /// </summary>
            /// <param name="colAL">一行</param>
            /// <returns></returns>
            private string ConvertToSaveLine(ArrayList colAL)
            {
                string saveLine;            saveLine = "";
                for (int i = 0; i < colAL.Count; i++)
                {
                    saveLine += ConvertToSaveCell(colAL[i].ToString());
                    //格子间以逗号分割
                    if (i < colAL.Count - 1)
                    {
                        saveLine += ",";
                    }
                }            return saveLine;
            }        /// <summary>
            /// 字符串转换成CSV中的格子
            /// 双引号转换成两个双引号,然后首尾各加一个双引号
            /// 这样就不需要考虑逗号及换行的问题
            /// </summary>
            /// <param name="cell">格子内容</param>
            /// <returns></returns>
            private string ConvertToSaveCell(string cell)
            {
                cell = cell.Replace("\"", "\"\"");            return "\"" + cell + "\"";
            }
        }
    }
      

  14.   

    我简单的给你写了一下思路,请参考,我在记事本里写的,不能编译通过,要用我上面给你的类        private void Txt2CSV()
            {  
                string filePath = "";//你的txt文档的路径
                private CsvStreamWriter csvW = new CsvStreamWriter();//实例化写CSV类
                StreamReader smRead = new StreamReader(filePath, System.Text.Encoding.Default);
                string line;
                while ((line = smRead.ReadLine()) != null)
                {
                    string[] arrStr = line.Split(' ');
                    string nIndex = arrStr[0].ToString();
                    string nBlank = arrStr[1].ToString();
                    string data1 = arrStr[2].ToString();
                    string data2 = arrStr[3].ToString();
                    string data3 = arrStr[4].ToString();
                    string data4 = arrStr[5].ToString();
                    string data5 = arrStr[6].ToString();
                    string data6 = arrStr[7].ToString();
                    string data7 = arrStr[8].ToString();                csvW[1,2] = nIndex; //将nIndex值写入csv文件的第一行第二列(B1单元格)依次写下去
                    ......            }
                    string csvPath="";//定义CSV文件的路径
                    csvW.FileName=csvPath;
                    csvW.Save()//保存CSV
            }
      

  15.   

    上面是针对你的b.txt的,我不知道a.txt你要导成什么样的excel?一行导入一个单元格?无论怎样,上面给了你思路如何读取txt
    如何根据特定分隔符拆分字符串
    如何导入CSV
      

  16.   

    ttiimm11
    哥哥,我按照您给的代码写的,出来的数据就最后一行循环呢?还有一堆分号不知道哪儿出来的,还望指教,多谢!