簡體   English   中英

C#Excel在RTF中格式化每一行

[英]c# excel format each row in rtf

我正在開發一個必須將每行中的所有貴重物品轉換為rtf文檔的應用程序。 我正在使用的Excel文件有10.000多行,但只有5列。 到目前為止,我可以使用Value2.ToString方法讀取文件。 我想做的是從每一行中獲取5個值,並為它們提供一個標題

firsttext = cel a1的文本

secondtext = cel b1的文本

thirdtext = cel c1的文本

fourtext = cel d1的文本

fifttext = cel e1的文本

並對cel a2,b2等執行相同的操作。到目前為止,我的代碼是

           //Create COM Objects. 
        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\aaa.xlsx");
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;  
        int rowCount = xlRange.Rows.Count;
        int colCount = xlRange.Columns.Count;

        //iterate over the rows and columns and print to the console as it appears in the file
        //excel is not zero based!!
        for (int i = 1; i <= rowCount; i++)
        {
            for (int j = 1; j <= colCount; j++)
            {
                //new line
                if (j == 1)
                    Console.WriteLine("\r\n");

                //write the value to the console
                if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                    Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t", "\r\n");

            }

           // Console.ReadLine(); for testing purposes         
        }

        //cleanup

        GC.Collect();
        GC.WaitForPendingFinalizers();

        //  rule of thumb for releasing com objects:
        //  never use two dots, all COM objects must be referenced and released individually
        //  ex: [somthing].[something].[something] is bad

        //release com objects to fully kill excel process from running in the background
        Marshal.ReleaseComObject(xlRange);
        Marshal.ReleaseComObject(xlWorksheet);

        //close and release
        xlWorkbook.Close();
        Marshal.ReleaseComObject(xlWorkbook);

        //quit and release
        xlApp.Quit();
        Marshal.ReleaseComObject(xlApp);
        Console.ReadLine();

任何幫助,將不勝感激

我仍然不太確定您的問題是什么?

您是在問如何添加標題嗎?

//iterate over the rows and columns and print to the console as it appears in the file
//excel is not zero based!!
for (int i = 1; i <= rowCount; i++)
{
    for (int j = 1; j <= colCount; j++)
    {
        //new line
        if (j == 1)
            Console.WriteLine("\r\n");

        //write the value to the console
        if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
           Console.Write("{0}= ", GetHeaderText(j));
           Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t", "\r\n");
    }
   // Console.ReadLine(); for testing purposes         
}

通過一些輔助方法獲取標題文本:

private string GetHeaderText(int colId)
{
    switch (colId)
    {
        case 1:
           return "firsttext";
        case 2:
           return "secondtext";
        case 3:
           return "thirdtext";
        case 4:
           return "fourthtext";
        case 5:
           return "fithtext";
        default:
           return "header not defined";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM