我使用在此特定网站上找到的代码成功显示了数据。问题是,字体的颜色,粗体或斜体不会被转移。我需要读取特定单元格的代码并将其转换为HTML字符串并在其他地方将其显示为文本框。所以我真的需要学习如何导入颜色,粗体或斜体,所以我也可以转换它。我将永远感激任何帮助我的灵魂。以下是我目前的代码。
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;
答案 0 :(得分:0)
以下打开app文件夹中的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;
}
}
}
}
表单代码,可以通过按钮工作并具有面板 var demoExcel = new ExcelCode(); //在app文件夹中打开Excel文件,从Sheet1中的H2获取信息 textBox1.Text = demoExcel .GetCellValue( Path.Combine(AppDomain.CurrentDomain.BaseDirectory,“Demo.xlsx”), “工作表Sheet1” “H2”);
// 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());