79 lines
2.3 KiB
C#
79 lines
2.3 KiB
C#
using System.Threading.Tasks;
|
||
using SnippingTool;
|
||
using System.Windows.Forms;
|
||
using Application = Microsoft.Office.Interop.Excel.Application;
|
||
|
||
namespace RhSolutions.Tools;
|
||
|
||
internal class OcrTool : ITool
|
||
{
|
||
private IOcrClient client = RhSolutionsAddIn.ServiceProvider.GetService<IOcrClient>();
|
||
private Application app = RhSolutionsAddIn.Excel;
|
||
private string xFolderId = RhSolutionsAddIn.Configuration["x-folder-id"];
|
||
private string apiKey = RhSolutionsAddIn.Configuration["apiKey"];
|
||
|
||
public async void Execute()
|
||
{
|
||
try
|
||
{
|
||
RhSolutionsAddIn.Excel.Visible = false;
|
||
Task.Run(async delegate
|
||
{
|
||
await Task.Delay(250);
|
||
}).Wait();
|
||
|
||
string shot = Snipper.SnipBase64();
|
||
RhSolutionsAddIn.Excel.Visible = true;
|
||
|
||
if (shot != null)
|
||
{
|
||
IEnumerable<object[,]> tables = await client.ProcessImage(shot);
|
||
if (tables != null)
|
||
{
|
||
foreach (var table in tables)
|
||
{
|
||
int rowCount = table.GetLength(0);
|
||
int columnCount = table.GetLength(1);
|
||
|
||
Range currentCell = app.ActiveCell;
|
||
Range tableRange = app.ActiveSheet.Range(currentCell,
|
||
app.ActiveSheet.Cells(currentCell.Row + rowCount - 1, currentCell.Column + columnCount - 1));
|
||
|
||
if (app.WorksheetFunction.CountA(tableRange) > 0)
|
||
{
|
||
MessageBox.Show(@"На листе отсустствует диапазон для вставки распознанной таблицы.Попробуйте в другом месте или на пустом листе.",
|
||
"Ошибка",
|
||
MessageBoxButtons.OK,
|
||
MessageBoxIcon.Error);
|
||
RhSolutionsAddIn.Excel.Visible = true;
|
||
return;
|
||
}
|
||
tableRange.Borders.LineStyle = XlLineStyle.xlContinuous;
|
||
|
||
for (int row = 0; row < rowCount; row++)
|
||
for (int column = 0; column < columnCount; column++)
|
||
{
|
||
Range excelCell = app.ActiveSheet.Cells(currentCell.Row + row,
|
||
currentCell.Column + column);
|
||
excelCell.Value2 = table[row,column];
|
||
excelCell.EntireColumn.AutoFit();
|
||
excelCell.EntireRow.AutoFit();
|
||
}
|
||
app.ActiveSheet.Cells(currentCell.Row + rowCount + 1, currentCell.Column).Activate();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
throw;
|
||
}
|
||
finally
|
||
{
|
||
RhSolutionsAddIn.Excel.Visible = true;
|
||
}
|
||
}
|
||
public void Dispose()
|
||
{
|
||
}
|
||
} |