Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将多个文本文件转换为一个excel文件,并在C#中的文件内容之间留有一个单元格空间?_C#_Excel_Vba_Visual Studio_Text - Fatal编程技术网

如何将多个文本文件转换为一个excel文件,并在C#中的文件内容之间留有一个单元格空间?

如何将多个文本文件转换为一个excel文件,并在C#中的文件内容之间留有一个单元格空间?,c#,excel,vba,visual-studio,text,C#,Excel,Vba,Visual Studio,Text,不要被文字墙吓倒!!!问题本身并不太复杂,我的问题只是需要大量的上下文 我有多个带文本的excel文件,每个文件有两个版本,其中一个版本正在修改。我现在拥有的是一个程序,它将一对文件的所有差异编译成一个文件,然后是一个将这些修订写入excel文件的程序。 例: 我想要的是一个程序,它接收所有不同的文件对,并输出一个excel,其修订版由一个单元格空间分隔(表示不同的文件对),理想情况下是一个合并我下面两个程序的程序 例如,如果与上面文件1的v1和v2一起输入文件2: file 2 (v1) 5

不要被文字墙吓倒!!!问题本身并不太复杂,我的问题只是需要大量的上下文 我有多个带文本的excel文件,每个文件有两个版本,其中一个版本正在修改。我现在拥有的是一个程序,它将一对文件的所有差异编译成一个文件,然后是一个将这些修订写入excel文件的程序。 例:

我想要的是一个程序,它接收所有不同的文件对,并输出一个excel,其修订版由一个单元格空间分隔(表示不同的文件对),理想情况下是一个合并我下面两个程序的程序

例如,如果与上面文件1的v1和v2一起输入文件2:

file 2 (v1)
5
6
7
8

file 2 (v2)
5
7
6
8
我想要输出的是

line 1: 1 (in cell A1)
line 2: 2 (in cell A2)

line 2: 6 (in cell A4)
line 3: 7 (in cell A5)
这是我的密码:

   internal class Program
{

    private static void AddDataFromFile(_Worksheet excellWorkSheet, string fileName)
    {
        fileName = @"C: \Users\result2.txt";
        if (excellWorkSheet == null)
        {
            throw new ArgumentNullException("excellWorkSheet");
        }

        var lines = File.ReadAllLines("result2.txt");
        var rowCounter = 1;
        foreach (var line in lines)
        {
            var columnCounter = 1;
            var values = line.Split(Environment.NewLine.ToCharArray());
            foreach (var value in values)
            {
                excellWorkSheet.Cells[rowCounter, columnCounter] = value;
                columnCounter++;
            }

            rowCounter++;
        }
    }

    private static void CloseQuitAndRelease(
        Microsoft.Office.Interop.Excel.Application excellApp, Worksheet excellWorkSheet, Workbook excellWorkBook, object misValue)
    {
        excellWorkBook.Close(true, misValue, misValue);
        excellApp.Quit();
        ReleaseObject(excellApp);
        ReleaseObject(excellWorkBook);
        ReleaseObject(excellWorkSheet);
    }

    /// <summary>
    /// The main.
    /// </summary>
    /// <param name="args" />
    /// The args. 
    /// 
    private static void Main(string[] args)
    {
        object misValue = System.Reflection.Missing.Value;
        var excellApp = new Microsoft.Office.Interop.Excel.Application();
        var excellWorkBook = excellApp.Workbooks.Add(misValue);

        var excellWorkSheet = (Worksheet)excellWorkBook.Sheets["Sheet1"];


        /* Data(excellWorkSheet); */
        const string InputfileName = "result2.txt";
        AddDataFromFile(excellWorkSheet, InputfileName);
        Safe(misValue, excellWorkBook, "result-informations.xls");

        CloseQuitAndRelease(excellApp, excellWorkSheet, excellWorkBook, misValue);

        Console.WriteLine("File created !");
    }

    private static void ReleaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to release the Object " + ex);
        }
        finally
        {
            GC.Collect();
        }
    }

    private static void Safe(object misValue, Workbook excellWorkBook, string excellfilename)
    {
        excellWorkBook.SaveAs(
            excellfilename,
            XlFileFormat.xlWorkbookNormal,
            misValue,
            misValue,
            misValue,
            misValue,
            XlSaveAsAccessMode.xlExclusive,
            misValue,
            misValue,
            misValue,
            misValue,
            misValue);
    }
内部类程序
{
私有静态void AddDataFromFile(_工作表excellWorkSheet,字符串文件名)
{
fileName=@“C:\Users\result2.txt”;
如果(ExcelWorkSheet==null)
{
抛出新ArgumentNullException(“ExcelWorkSheet”);
}
var lines=File.ReadAllLines(“result2.txt”);
var rowCounter=1;
foreach(行中的var行)
{
var列计数器=1;
var值=line.Split(Environment.NewLine.ToCharArray());
foreach(值中的var值)
{
Cells[rowCounter,columnCounter]=值;
列计数器++;
}
行计数器++;
}
}
私有静态无效关闭和释放(
Microsoft.Office.Interop.Excel.Application excellApp、工作表ExcelWorkSheet、工作簿ExcelWorkBook、对象错误值)
{
excellWorkBook.Close(true、misValue、misValue);
excellApp.Quit();
释放对象(excellApp);
发布对象(ExcelWorkbook);
发布对象(Excel工作表);
}
/// 
///主要的。
/// 
/// 
///args。
/// 
私有静态void Main(字符串[]args)
{
对象错误值=System.Reflection.Missing.Value;
var excellApp=new Microsoft.Office.Interop.Excel.Application();
var excellWorkBook=excellApp.Workbooks.Add(misValue);
var excellWorkSheet=(工作表)excellWorkBook.Sheets[“Sheet1”];
/*数据(Excel工作表)*/
常量字符串InputfileName=“result2.txt”;
AddDataFromFile(Excel工作表,InputfileName);
安全(错误值,Excel工作簿,“结果信息.xls”);
关闭和释放(excellApp、ExcelWorkSheet、ExcelWorkBook、misValue);
WriteLine(“文件已创建!”);
}
私有静态无效释放对象(对象obj)
{
尝试
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
}
捕获(例外情况除外)
{
Console.WriteLine(“无法释放对象”+ex);
}
最后
{
GC.Collect();
}
}
私有静态void-Safe(对象值错误、工作簿ExcelWorkbook、字符串ExcelFileName)
{
excellWorkBook.SaveAs(
卓越的名字,
XlFileFormat.xlWorkbookNormal,
误判,
误判,
误判,
误判,
XlSaveAsAccessMode.xlExclusive,
误判,
误判,
误判,
误判,
错误价值);
}
我的代码将差异编译为一个文本文件:

private void button_compare_Click(object sender, EventArgs e)
    {

        String directory = @"C:\";
        String[] linesA = File.ReadAllLines(Path.Combine(directory, "test1.txt"));
        String[] linesB = File.ReadAllLines(Path.Combine(directory, "test2.txt"));


        List<string> onlyB = new List<string>();

        for (int i = 0; i < linesA.Length; i++)
        {
            if (!linesA[i].Equals(linesB[i]))
            {
                onlyB.Add("line " + i + ": " + string.Join(" ", linesB[i].Split(' ').Except(linesA[i].Split(' '))));
            }
        }
        onlyB.Add("line " + linesB.Length + ": " + linesB.Last());
        File.WriteAllLines(Path.Combine(directory, "Result2.txt"), onlyB);

    }
private void按钮\u比较\u单击(对象发送者,事件参数e)
{
字符串目录=@“C:\”;
字符串[]linesA=File.ReadAllLines(Path.Combine(目录,“test1.txt”);
String[]linesB=File.ReadAllLines(Path.Combine(目录,“test2.txt”);
List onlyB=新列表();
对于(int i=0;i
您可以尝试使用OpenXml创建excel文件,这里有一个问题,您的代码中有什么不起作用?问题/错误在哪里等?@dee,不是有错误,我想知道如何调整代码以满足我的查询。在这种情况下,最好在此处发布此问题。
private void button_compare_Click(object sender, EventArgs e)
    {

        String directory = @"C:\";
        String[] linesA = File.ReadAllLines(Path.Combine(directory, "test1.txt"));
        String[] linesB = File.ReadAllLines(Path.Combine(directory, "test2.txt"));


        List<string> onlyB = new List<string>();

        for (int i = 0; i < linesA.Length; i++)
        {
            if (!linesA[i].Equals(linesB[i]))
            {
                onlyB.Add("line " + i + ": " + string.Join(" ", linesB[i].Split(' ').Except(linesA[i].Split(' '))));
            }
        }
        onlyB.Add("line " + linesB.Length + ": " + linesB.Last());
        File.WriteAllLines(Path.Combine(directory, "Result2.txt"), onlyB);

    }