我是使用EPPlus的新手。
我想要一个单元格,相对于一个起点,实现这个目标的最佳方法是什么?
例如:
var startLocation = "C5";
var newLocation1 = TLoc(startLocation, 2, 3); //E8
var newLocation2 = TLoc(startLocation, 0, 1); //C6
现在我自己写了这个方法,因为我不熟悉EPPlus功能:
/// <summary>
/// Use EPPlus to translate the Location.
/// </summary>
/// <param name="startLocation"></param>
/// <param name="columns"></param>
/// <param name="rows"></param>
/// <returns></returns>
private string TLoc(string startLocation, int columns, int rows)
{
using (ExcelPackage tmpPack = new ExcelPackage())
{
var tmpWs = tmpPack.Workbook.Worksheets.Add("temp");
var startCell = tmpWs.Cells[startLocation];
int startColumn = startCell.Start.Column;
int startRow = startCell.Start.Row;
int tColumn = startColumn + columns;
int tRow = startRow + rows;
ExcelAddress translatedAddress = new ExcelAddress(tRow, tColumn, tRow, tColumn);
return translatedAddress.ToString();
}
}
请给我一个例子或链接我的文档。谢谢!
更具体一点:使用EPPlus实现此功能的示例。
我喜欢在可用时使用经过验证的技术,而不是冒着引入新bug的风险。
答案 0 :(得分:1)
我希望这对你有所帮助,我不清楚你的问题。 但我希望你能找到这样的东西。
以下是将整数行转换为整数列到Excel地址的解决方案。
您需要在数据返回Excel工作表之前和之后创建地址
public class ExcelAddress
{
public readonly string LabelCell;
public readonly string ColumnAddress;
public readonly int ColumnAddressNumber;
public readonly int RowAddressNumber;
public readonly string RowAddress;
public readonly string CellAddress; // this will give you address
public ExcelAddress(int colNum, int rowNum, string Label)
{
ColumnAddressNumber = colNum;
ColumnAddress = ExcelCellAddressConvertor.FromIntegerIndexToColumnLetter(ColumnAddressNumber);
RowAddressNumber = rowNum;
RowAddress = RowAddressNumber.ToString();
LabelCell = Label;
CellAddress = ColumnAddress + RowAddress;
}
}
以下是用于地址映射的转换器类
using System;
using System.Text.RegularExpressions;
namespace BLABLA
{
public static class ExcelCellAddressConvertor
{
public static string FromIntegerIndexToColumnLetter(int intCol)
{
var intFirstLetter = ((intCol) / 676) + 64;
var intSecondLetter = ((intCol % 676) / 26) + 64;
var intThirdLetter = (intCol % 26) + 65;
var firstLetter = (intFirstLetter > 64)
? (char)intFirstLetter : ' ';
var secondLetter = (intSecondLetter > 64)
? (char)intSecondLetter : ' ';
var thirdLetter = (char)intThirdLetter;
return string.Concat(firstLetter, secondLetter,
thirdLetter).Trim();
}
public static int ConvertColumnNameToNumber(string columnName)
{
var alpha = new Regex("^[A-Z]+$");
if (!alpha.IsMatch(columnName)) throw new ArgumentException();
char[] colLetters = columnName.ToCharArray();
Array.Reverse(colLetters);
var convertedValue = 0;
for (int i = 0; i < colLetters.Length; i++)
{
char letter = colLetters[i];
// ASCII 'A' = 65
int current = i == 0 ? letter - 65 : letter - 64;
convertedValue += current * (int)Math.Pow(26, i);
}
return convertedValue;
}
}
}
用于Excel自动化的EEPLUS GOOD API,
但仍然简单明了一个
答案 1 :(得分:1)
对于EPPlus
特定解决方案,我能想到的唯一方法是“最短”是这样的:
//Prints "E8"
Console.WriteLine(ExcelCellBase.TranslateFromR1C1(ExcelCellBase.TranslateToR1C1("C5", -3, -2), 0, 0));
//Prints "C8"
Console.WriteLine(ExcelCellBase.TranslateFromR1C1(ExcelCellBase.TranslateToR1C1("C5", -1, 0), 0, 0));
不是最干净但是由于它们的制造者没有暴露支持这两种方法的翻译功能,我们不能直接使用它。