C#转换MS Excel中柱为UTF-8,然后为base64 [英] C# Convert MS Excel column to utf-8 and then to base64
本文介绍了C#转换MS Excel中柱为UTF-8,然后为base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个情况我有到Excel中单列转换为UTF-8,然后为base64。
我已经经历了几个后这说明了如何读取Excel文件了。
阅读Excel中从C#
文件
但我不知道,在我的情况,这是最好的办法。
我有2MB的大文件。
请给我建议。
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.
推荐答案
我得到了解决
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++;
}
}
}
}
这篇关于C#转换MS Excel中柱为UTF-8,然后为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文