Luc*_*kyS 14 c# asp.net oledb excel winforms
如何通过oledb阅读器在Excel中检查单元格是否包含公式?
System.Data.OleDb.OleDbConnection conn2 = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + "; Extended Properties = \"Excel 8.0;HDR=NO;IMEX=1\";");
conn2.Open();
string strQuery2 = "SELECT * FROM [" + Table + "]";
System.Data.OleDb.OleDbDataAdapter adapter2 = new System.Data.OleDb.OleDbDataAdapter(strQuery2, conn2);
System.Data.DataTable DT2 = new System.Data.DataTable();
adapter2.Fill(DT2);
Run Code Online (Sandbox Code Playgroud)
你可以探索一下:Range.HasFormula
下com-interop
.
我还注意到有一个帖子可以即兴创作来满足你的需求.
这是一个骨架 - 而不是确切的语法.
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workBook = excelApp.Workbooks.Open(filePath);
Excel.WorkSheet WS = workBooks.WorkSheets("Sheet1");
Range rangeData = WS.Range["A1:C3"];
foreach (Excel.Range c in rangeData.Cells)
{
if (c.HasFormula)
{
MessageBox.Show(Convert.ToString(c.Value));
}
}
Run Code Online (Sandbox Code Playgroud)
不确定如何实现这一点OLEDB
,因为您的查询似乎只是将单元格数据(文本,数字,没有公式)抓取到查询中.
如果你必须使用OLEDB
,这篇文章可以帮助你开始.如果您仍需要帮助,请随时发表评论.
您可以使用OpenXML SDK读取 Xlsx 文件。
为此,您需要添加对 OpenXML 库的引用,这可以通过nuget 包完成(您还需要对 WindowsBase 的引用)。然后,您需要加载电子表格,找到您感兴趣的工作表并迭代单元格。
每个单元格Cell
都有一个CellFormula
属性,如果该单元格中有公式,则该属性将为非空。
例如,以下代码将迭代每个单元格并为任何具有公式的单元格输出一行。true
如果任何单元格中有公式,它将返回;否则它将返回false
:
public static bool OutputFormulae(string filename, string sheetName)
{
bool hasFormula = false;
//open the document
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, false))
{
//get the workbookpart
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
//get the correct sheet
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).First();
if (sheet != null)
{
//get the corresponding worksheetpart
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id) as WorksheetPart;
//iterate the child Cells
foreach (Cell cell in worksheetPart.Worksheet.Descendants<Cell>())
{
//check for a formula
if (cell.CellFormula != null && !string.IsNullOrEmpty(cell.CellFormula.Text))
{
hasFormula = true;
Console.WriteLine("Cell {0} has the formula {1}", cell.CellReference, cell.CellFormula.Text);
}
}
}
}
return hasFormula;
}
Run Code Online (Sandbox Code Playgroud)
可以使用文件名和您感兴趣的工作表名称来调用它,尽管更新代码以迭代所有工作表很简单。调用示例:
bool formulaExistsInSheet = OutputFormulae(@"d:\test.xlsx", "Sheet1");
Console.WriteLine("Formula exists? {0}", formulaExistsInSheet);
Run Code Online (Sandbox Code Playgroud)
上面的输出示例:
单元格 C1 具有公式 A1+B1
单元格 B3 具有公式 C1*20
公式存在吗?真的
如果您只对工作表中是否有任何具有公式的单元格感兴趣,则可以使用Any
扩展方法来简化上述内容:
public static bool HasFormula(string filename, string sheetName)
{
bool hasFormula = false;
//open the document
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, false))
{
//get the workbookpart
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
//get the correct sheet
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).First();
if (sheet != null)
{
//get the corresponding worksheetpart
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id) as WorksheetPart;
hasFormula = worksheetPart.Worksheet.Descendants<Cell>().Any(c =>
c.CellFormula != null && !string.IsNullOrEmpty(c.CellFormula.Text));
}
}
return hasFormula;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4409 次 |
最近记录: |