C#将16进制颜色excel批量转换为rgb255,255,255类型
界面如下
数据表
代码(需要添加Microsoft.Office.Interop.Excel.dll引用 另存时用到)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 读取csv
{
public partial class readExcel16进制转10进制 : Form
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public readExcel16进制转10进制()
{
InitializeComponent();
}
//方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下:
public DataSet ExcelToDS(string Path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = new DataSet();
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
return ds;
}
private void button1_Click(object sender, EventArgs e)//读取
{
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == DialogResult.OK)
{
string fileName = openFile.FileName;
ds = ExcelToDS(fileName);
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
}
}
private void button2_Click(object sender, EventArgs e)//计算
{
for (int i = 0; i < dt.Rows.Count;i++ )
{
string col= dt.Rows[i][2].ToString();//2是代表列 不同位置不同值。
int r = Convert.ToInt32(col.Substring(0, 2), 16);
int g = Convert.ToInt32(col.Substring(2, 2), 16);
int b = Convert.ToInt32(col.Substring(4, 2), 16);
dt.Rows[i][3] = r;
dt.Rows[i][4] = g;
dt.Rows[i][5] = b;
dataGridView1.DataSource = dt;
}
}
private void btnsaveas_Click(object sender, EventArgs e)//另存
{
{
string fileName = "";
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
//写入标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;}
//写入数值
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{ for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);
if (saveFileName != "")
{
try
{workbook.Saved = true;
workbook.SaveCopyAs(saveFileName); //fileSaved = true;
}
catch (Exception ex)
{//fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
xlApp.Quit();
GC.Collect();//强行销毁
}
}
}
}