C# Convert MS Excel column to utf-8 and then to ba

2019-09-21 05:39发布

I have a situation where I have to convert excel single column to utf-8 and then to base64.
I have gone through several post which suggests how to read excel file.

Reading Excel files from C#
But I am not sure that in my situation which is the best approach.
I have big files of 2MB.
Kindly suggest me.

1条回答
疯言疯语
2楼-- · 2019-09-21 05:52

I got the solution

using System;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
         bgw.RunWorkerAsync();
        var myConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Language_Batch1_OneClick.xls';Extended Properties=Excel 8.0;"); ;
        var myCommand = new OleDbCommand();
        var upCommand = new OleDbCommand();
        int i = 0;
        try
        {

            string sql = null;
            myConnection.Open();
            myCommand.Connection = myConnection;
            sql = "select ANSWER_CODE,Punjabi from [Batch_Lang_1$]";
            myCommand.CommandText = sql;
            var dataReader = myCommand.ExecuteReader();

            while (dataReader.Read())
            {
                var langText = Convert.ToBase64String(Encoding.UTF8.GetBytes(dataReader["Punjabi"].ToString()));
                if (langText.Length >= 1000)
                {
                    continue;
                }
                var ansCode = dataReader["ANSWER_CODE"].ToString();
                sql = "update [Batch_Lang_1$] set Punjabi= '" + langText + "'  where ANSWER_CODE='" + ansCode + "'";
                upCommand.Connection = myConnection;
                upCommand.CommandText = sql;
                upCommand.ExecuteNonQuery();
                i++;
            }
    }
 }
}
查看更多
登录 后发表回答