我如何保存为Excel文件(.xlsb)在文本(.txt)(制表符分隔)格式使用Excel Interop c#

本文关键字:Excel txt 分隔 格式 制表符 Interop 文本 xlsb 何保存 保存 文件 | 更新日期: 2023-09-27 18:16:46

我想打开Excel文件并保存为文本选项卡分隔格式。对象如何在Save As()设置?

我如何保存为Excel文件(.xlsb)在文本(.txt)(制表符分隔)格式使用Excel Interop c#

如何做到这一点:

//The connection string to the excel file
   string connstr = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=d:'temp'test.xls;Extended Properties=Excel 8.0";
   //The connection to that file
   OleDbConnection conn = new OleDbConnection(connstr);
   //The query
   string strSQL = "SELECT * FROM [Feuil1$]";
   //The command 
   OleDbCommand cmd = new OleDbCommand(/*The query*/strSQL, /*The connection*/conn);
   DataTable dt = new DataTable();
   conn.Open();
   try
   {
     OleDbDataReader dr1 = cmd.ExecuteReader();
     StreamWriter sw = new StreamWriter("D:''temp''export.txt");
     if (dr1.Read())
     {
       dt.Load(dr1);
     }
     int iColCount = dt.Columns.Count;
     for (int i = 0; i < iColCount; i++)
     {
       sw.Write(dt.Columns[i]);
       if (i < iColCount - 1)
       {
         sw.Write("'t");
       }
     }
     sw.Write(sw.NewLine);
     // Now write all the rows.
     foreach (DataRow dr in dt.Rows)
     {
       for (int i = 0; i < iColCount; i++)
       {
         if (!Convert.IsDBNull(dr[i]))
         {
           sw.Write(dr[i].ToString());
         }
         if (i < iColCount - 1)
         {
           sw.Write("'t");
         }
       }
       sw.Write(sw.NewLine);
     }
     sw.Close();
     Console.WriteLine("File is saved");
   }
   catch (OleDbException caught)
   {
     Console.WriteLine(caught.Message);
   }
   finally
   {
     conn.Close();
   }

来源:Msdn