【问题标题】:How to insert a text into Excel file如何在 Excel 文件中插入文本
【发布时间】:2016-02-10 06:33:36
【问题描述】:

目的是使用 C# 将一张图像和随附的文本插入到 Excel 文件中,并带有图片的坐标。插入图片没问题,但我不能对任何文本这样做。

例如:图片坐标:top = 14f, left = 16f

我的插入图片代码:

Excel.Shape img = worksheet.Shapes(,,top,left,width,height);

我的插入文本代码:

worksheet.Cells[x,y] = "jdshfg";

这是Excel中使用行列插入一个单元格的文本功能。

我想从左上角(图像坐标)获取此单元格的 [x,y]。

【问题讨论】:

  • 对不起:worksheet.Cells[x,y] = "jdshfg";用于插入文本,而不是图像

标签: c# excel text coordinates worksheet


【解决方案1】:

如果我理解您的要求,您希望将 Excel 样式的行/列坐标转换为像素坐标。我在插入图表时遇到了同样的问题。下面的第一个函数将为您提供“x”,第二个函数将为您提供“y”。

public double CellRangeToPixelWidth(Worksheet sheet, int left, int right) {
  Range range = sheet.get_Range(CellString(left - 1, 1, right - 1, 1), Type.Missing);
  return range.Width;
}

public double CellRangeToPixelHeight(Worksheet sheet, int top, int bottom) {
  Range range = sheet.get_Range(CellString(0, top, 0, bottom), Type.Missing);
  return range.Height;
}

public string CellString(int left, int top, int right, int bottom) {
  return string.Format("{0}{1}:{2}{3}", CellString(left), top, CellString(right), bottom);
}

public string CellString(int col) {
  if (col < 26)
    return ((char)('A' + col)).ToString();
  else if (col < 27 * 26)
    return ((char)('A' + col / 26 - 1)).ToString() + ((char)('A' + col % 26)).ToString();
  return "";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    相关资源
    最近更新 更多