C# 显示Excel文件并将其读入datagridview
我已经使用在这个特定网站上找到的代码成功地显示了数据。问题是,字体的颜色、粗体或斜体并没有保留下来。我需要读取特定单元格的代码,将其转换为HTML字符串,并在其他地方显示为文本框。所以我真的需要学习如何导入颜色,粗体或斜体,这样我也可以转换它。我将永远感激在这件事上帮助我的任何善良的灵魂。下面是我当前的代码C# 显示Excel文件并将其读入datagridview,c#,excel,datagridview,C#,Excel,Datagridview,我已经使用在这个特定网站上找到的代码成功地显示了数据。问题是,字体的颜色、粗体或斜体并没有保留下来。我需要读取特定单元格的代码,将其转换为HTML字符串,并在其他地方显示为文本框。所以我真的需要学习如何导入颜色,粗体或斜体,这样我也可以转换它。我将永远感激在这件事上帮助我的任何善良的灵魂。下面是我当前的代码 string PathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";Extended
string PathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(PathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [sheet1$]", conn);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
下面将在应用程序文件夹中打开一个Excel文件,获取单元格值、前景色,查看是否为范围sheet1单元格H2设置了粗体和斜体。请注意,有相当数量的代码是有保证的,因此这里使用的所有对象在完成我们的工作后都会被发布
using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication1_CS
{
public class ExcelCode
{
public System.Drawing.Color ForeColor;
public ExcelCode() { }
public string GetCellValue(
string FileName,
string SheetName,
string CellAddress)
{
string CellValue = "";
if (System.IO.File.Exists(FileName))
{
bool Proceed = false;
Excel.Application xlApp = null;
Excel.Workbooks xlWorkBooks = null;
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;
Excel.Sheets xlWorkSheets = null;
Excel.Range xlCells = null;
xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
xlWorkBooks = xlApp.Workbooks;
xlWorkBook = xlWorkBooks.Open(FileName);
xlApp.Visible = false;
xlWorkSheets = xlWorkBook.Sheets;
for (int x = 1; x <= xlWorkSheets.Count; x++)
{
xlWorkSheet = (Excel.Worksheet)xlWorkSheets[x];
if (xlWorkSheet.Name == SheetName)
{
Proceed = true;
break;
}
System.Runtime.InteropServices
.Marshal.FinalReleaseComObject(xlWorkSheet);
xlWorkSheet = null;
}
if (Proceed)
{
xlCells = xlWorkSheet.Range[CellAddress];
try
{
Console.WriteLine("Bold: {0}", xlCells.Font.Bold); // bool
Console.WriteLine("Italic: {0}", xlCells.Font.Italic); // bool
ForeColor = System.Drawing.ColorTranslator.FromOle((int)xlCells.Font.Color);
CellValue = Convert.ToString(xlCells.Value);
}
catch (Exception)
{
// Reduntant
CellValue = "";
}
}
else
{
MessageBox.Show(SheetName + " not found.");
}
xlWorkBook.Close();
xlApp.UserControl = true;
xlApp.Quit();
ReleaseComObject(xlCells);
ReleaseComObject(xlWorkSheets);
ReleaseComObject(xlWorkSheet);
ReleaseComObject(xlWorkBook);
ReleaseComObject(xlWorkBooks);
ReleaseComObject(xlApp);
}
else
{
MessageBox.Show("'" + FileName +
"' not located. Try one of the write examples first.");
}
return CellValue;
}
public static void ReleaseComObject(object obj)
{
try
{
if (obj != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
}
catch (Exception)
{
obj = null;
}
}
}
}
可视化格式是Excel应用程序特有的,通过任何不是专门设计用于Excel的东西(如SQL)对其进行查询只会返回文本数据。您需要使用Office automation打开excel文件,循环您感兴趣的单元格,解析它们的格式值,并将它们转换为您需要的任何内容。
// Form has a panel, set back color to Sheet1.H2 color
panel1.BackColor = demoExcel.ForeColor;
// Translate the color to HTML
var htmlColor = System.Drawing.ColorTranslator.ToHtml(demoExcel.ForeColor);
Console.WriteLine(htmlColor.ToString());