我将如何在C#中编写代码以将Excel数据转换为文本文件。 [英] how will i write a code in C# to convert the excel data to text file.

查看:90
本文介绍了我将如何在C#中编写代码以将Excel数据转换为文本文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Excel数据转换为文本文件。


1.Excel包含5张。


2.来自每个表格我想要特定的颜色只显示在文本文件上。


3.我已经提到了App.config中的工作表名称,现在我将如何转换数据表中的数据发送短信



<请帮助!!



谢谢。

解决方案

您好onkardeshpande,


感谢您发布此处。


如有任何问题,请尝试以下代码。

 static void Main(string [] args)
{
//创建新的电子表格
电子表格文档=新的电子表格();
document.LoadFromFile(@" C:\ Users \v-wezan \Desktop\Output.xls");

//按名称获取工作表
工作表工作表= document.Workbook.Worksheets.ByName(" Demo");

List< string> text = new List< string>();
string s = string.Empty;
//检查日期
for(int i = 0; i< 2; i ++)
{
for(int j = 0; j< 2; j ++)
{
//设置当前单元
Cell currentCell = worksheet.Cell(i,j);
if(currentCell!= null)
{
//写入日期
s + = currentCell +" \t\t" ;;
//Console.Write("{0}{1}" ;, currentCell," \t\t");
}

}
text.Add(s);
s = string.Empty;
Console.WriteLine(" \ n");
}

//关闭文件
document.Close();

//将字符串写入文件。
//System.IO.File.WriteAllText(@"C:\Users\v-wezan\Desktop\test.txt" ;, text.ToString());
System.IO.StreamWriter file = new System.IO.StreamWriter(@" C:\ Users\v-wezan \Desktop\test.txt");
foreach(文本中的var项)
{
file.WriteLine(item);
}
file.Close();
}

最好的问候,


Wendy


I want to convert the excel data to text file.

1.Excel contains 5 sheets.

2.from each sheet i want specific coloumns only to display on text file.

3.I have mentioned the sheet  name in App.config with key now how would i convert the data that came in datatable to text file. 

its urgent

please help!!

Thank you.

解决方案

Hi onkardeshpande,

Thank you for posting here.

For your question, please try the following code.

 static void Main(string[] args)
        {
            // Create new Spreadsheet
            Spreadsheet document = new Spreadsheet();
            document.LoadFromFile(@"C:\Users\v-wezan\Desktop\Output.xls");

            // Get worksheet by name
            Worksheet worksheet = document.Workbook.Worksheets.ByName("Demo");

            List<string> text = new List<string>();
            string s=string.Empty;
            // Check dates
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    // Set current cell
                    Cell currentCell = worksheet.Cell(i, j);
                    if (currentCell!=null)
                    {
                        // Write Date                       
                        s += currentCell + "\t\t";                    
                        //Console.Write("{0}{1}", currentCell, "\t\t");
                    }
                    
                }
                text.Add(s);
                s = string.Empty;
                Console.WriteLine("\n");
            }

            // Close document
            document.Close();

            // Write the string to a file.
            //System.IO.File.WriteAllText(@"C:\Users\v-wezan\Desktop\test.txt", text.ToString());
            System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\v-wezan\Desktop\test.txt");
            foreach (var item in text)
            {
                file.WriteLine(item);
            }
            file.Close();
        }

Best Regards,

Wendy


这篇关于我将如何在C#中编写代码以将Excel数据转换为文本文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆