我正在開發一個項目,我需要用某些數據填充一些名稱範圍的excel。但是,我收到這些命名範圍的地址作爲字符串,而不是Microsoft.Office.Tools.Excel.NamedRange
。這些字符串看起來像Sheet1_A1_D1
。我如何將這些字符串轉換爲NamedRange對象?有沒有預定義的方法?感謝你的幫助!如何將字符串轉換爲excel命名的範圍?
-1
A
回答
0
如果字符串具有一致的模式,您可以將其拆分爲一個數組,其中包含定位所需命名範圍所需的信息。
一旦你的表名稱和地址的細胞,可以通過命名區域的片材進行迭代,並與從該字符串中提取的數據進行比較,每個命名範圍的地址:
private const int SHEET_ARRAY_INDEX = 0;
private const int RANGE_START_ARRAY_INDEX = 1;
private const int RANGE_END_ARRAY_INDEX = 2;
static void Main(string[] args)
{
string myString = "Sheet1_A1_A2";
string[] rangeInfo = myString.Split('_');
string sheet = rangeInfo[SHEET_ARRAY_INDEX]; // holds "Sheet1"
string arrayStart = rangeInfo[RANGE_START_ARRAY_INDEX]; // holds "A1"
string arrayEnd = rangeInfo[RANGE_END_ARRAY_INDEX]; // holds "A2"
}
對於通過命名迭代範圍請參見:https://bishoponvsto.wordpress.com/2009/09/01/reading-named-ranges-in-excel-using-c/
0
它看起來凌亂,我打賭有一個更簡單/更好的方式來做到這一點......但這個被它做
string sheetRange = "DataSheet1_A1_D1";
string[] splitStringArray = sheetRange.Split('_');
string sheetToGet = splitStringArray[0];
string firstCell = splitStringArray[1];
string lastCell = splitStringArray[2];
// Use the specified sheet
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[sheetToGet];
// Get the full range to work with
Range currentRange = xlWorkSheet.get_Range(firstCell + ":" + lastCell, Type.Missing);
// Note here:
// You could use "A","B","C"... etc to specify a column
// However... When the range is returned... The returned range will start on Col "A"
// so the given values wont match anyway...
// However all we need to know though is how many columns are involved
int colCount = currentRange.Columns.Count;
// the variables below are for testing the output
int curRow = 1;
int curCol = 1;
foreach (Range row in currentRange.Rows)
{
curCol = 1;
for (int i = 1; i <= colCount; i++)
{
((Excel.Range)row.Cells[1, i]).Value2 = "Row: " + curRow + " Column: " + curCol;
curCol++;
}
curRow++;
}
希望這有助於
0
你可以得到的名稱指的是像現在這樣的範圍內:
range = _excelApp.ActiveWorkbook.Names.OfType<Name>().SingleOrDefault(n => n.Visible && n.Name == "rangeName")?.RefersToRange;
另一種方法是讓Excel來解析地址:
range = _excelApp.get_Range(address);
這就要求地址以下列格式之一指定:
- B4
- 工作表Sheet1 B4
- 工作表Sheet1!A1:!D1
- namedRangeName
選項3似乎是因爲你的情況的路要走,Sheet1_A1_D1
似乎並沒有成爲一個指定的範圍,看起來像一個時髦的地址字符串。我建議你用'!'替換第一個下劃線和第二個':'並使用excelApp.get_range
方法。
相關問題
- 1. 試圖將excel命名的字符串範圍(帶空格的文本)轉換爲字符串數組
- 2. excel vba將字符串轉換爲範圍
- 3. 如何將字符串轉換爲範圍對象
- 4. rangeOfComposedCharacterSequencesForRange將0字符範圍轉換爲1個字符範圍
- 5. Excel VBA:命名範圍+字符串值作爲驗證列表?
- 6. Excel 2010動態命名範圍替換爲計算範圍
- 7. 如何將數字範圍的字符串轉換爲Python中的列表
- 8. 將範圍轉換爲VBA中的字符串
- 9. 將圖轉換爲規範字符串
- 10. 將範圍或DocumentFragment轉換爲字符串
- 11. 將字符串錶轉換爲int範圍
- 12. Excel中的命名範圍
- 13. 如何將unicode代碼點範圍轉換爲NSString字符範圍?
- 14. 如何將日期範圍轉換爲swift 3中的字符串?
- 15. 將數字轉換爲數字範圍?
- 16. 將值範圍轉換爲名稱
- 17. Excel Interop - 如何更改命名範圍
- 18. 如何將字符串的字符串轉換爲字符?
- 19. 如何在Excel的PowerQuery中將整數轉換爲字符串
- 20. 如何將字符串轉換爲Excel中的日期?
- 21. 如何將字符串轉換爲Excel中的日期/時間?
- 22. Excel:命名範圍與VBA
- 23. 將字符串轉換爲類名稱
- 24. 如何將方法轉換爲範圍。
- 25. rails將類方法轉換爲命名範圍
- 26. 如何將.NET標準格式字符串轉換爲Excel格式字符串
- 27. 將字符串轉換爲字符串
- 28. 如何將命令行字符串轉換爲C++中的hwnd?
- 29. 如何將字符串轉換爲c中的命令?
- 30. 如何將字符串的命令輸出轉換爲整數?