Guest User

Untitled

a guest
Jan 17th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. static string GetColumnLetter(int columnNumber)
  2. {
  3. var dividend = columnNumber;
  4. var columnName = String.Empty;
  5.  
  6. while (dividend > 0)
  7. {
  8. var modulo = (dividend - 1) % 26;
  9. columnName = Convert.ToChar(65 + modulo) + columnName;
  10. dividend = ((dividend - modulo) / 26);
  11. }
  12.  
  13. return columnName;
  14.  
  15. }
  16.  
  17.  
  18.  
  19. // Insert into Excel
  20. public static void ExcelInsert(string mFunction, string mColor, int mQty, string mFau, string mPrice)
  21. {
  22. var values = new List<string>
  23. {
  24. mFunction,
  25. mColor,
  26. mQty.ToString(),
  27. mFau,
  28. mPrice
  29. }.ToArray();
  30.  
  31. var rowNumber = Globals.ThisAddIn.Application.ActiveCell.Row;
  32. var columnNumber = Globals.ThisAddIn.Application.ActiveCell.Column;
  33. var columnLetter = GetColumnLetter(columnNumber);
  34.  
  35. for (var i = 0; i < values.Count(); i++ )
  36. {
  37. var range = Globals.ThisAddIn.Application.Range[String.Format("{0}{1}", columnLetter, rowNumber)];
  38. range.Value = values[i];
  39. columnNumber++;
  40. columnLetter = GetColumnLetter(columnNumber);
  41. }
  42. }
  43.  
  44. using Excel = Microsoft.Office.Interop.Excel;
  45.  
  46. void MyMethod()
  47. {
  48. //Replace '7' with the number of fields on your Windows Form
  49. int numberOfFields = 7;
  50.  
  51. string[] array = new string[numberOfFields];
  52.  
  53. array[0] = textBoxOneValue;
  54. array[1] = textBoxTwoValue;
  55. array[2] = textBoxThreeValue;
  56. array[3] = textBoxFourValue;
  57. array[4] = textBoxFiveValue;
  58. array[5] = textBoxSixValue;
  59. array[6] = textBoxSevenValue;
  60.  
  61. Excel.Application application = new Excel.Application();
  62. Excel.Workbook workbook = application.Workbooks.Open(@"C:whatever.xlsx");
  63. Excel.Worksheet worksheet = workbook.ActiveSheet;
  64.  
  65. Excel.Range activeCell = application.ActiveCell;
  66.  
  67. int rowNumber = activeCell.Row;
  68. int columnNumber = activeCell.Column;
  69.  
  70. string columnLetter = GetColumnLetter(columnNumber);
  71.  
  72. for(int i = 0; i < numberOfFields; i++)
  73. {
  74. Excel.Range range = worksheet.get_Range(String.Format("{0}{1}", columnLetter, rowNumber));
  75. range.Value = array[i];
  76. columnNumber++;
  77. columnLetter = GetColumnLetter(columnNumber);
  78. }
  79. }
  80.  
  81. string GetColumnLetter()
  82. {
  83. int dividend = columnNumber;
  84. string columnName = String.Empty;
  85. int modulo;
  86.  
  87. while (dividend > 0)
  88. {
  89. modulo = (dividend - 1) % 26;
  90. columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
  91. dividend = (int)((dividend - modulo) / 26);
  92. }
  93.  
  94. return columnName;
  95.  
  96. }
  97.  
  98. ActiveCell.Resize(, UBound(arr) - LBound(arr) + 1).Value = arr
  99.  
  100. Dim x(3) As String ' your array
  101. x(0) = "abc"
  102. x(1) = "def"
  103. x(2) = "ghi"
  104. x(3) = "jkl"
  105.  
  106. Dim r As Range ' range to paste
  107. Set r = ActiveCell
  108.  
  109. For Each s In x
  110. r.Value = s
  111. Set r = r.Offset(,1) ' keep going right
  112. Next
  113.  
  114. Dim x(4) As String ' this can actually contain 5 items, from 0 to 4
  115. x(1) = "abc"
  116. x(2) = "def"
  117. x(3) = "ghi"
  118. x(4) = "jkl"
  119.  
  120. Dim r As Range ' range to paste
  121. Set r = ActiveCell
  122.  
  123. For i = 1 To UBound(x) ' skip 0
  124. r.Value = x(i)
  125. Set r = r.Offset(, 1) ' keep going right
  126. Next
Add Comment
Please, Sign In to add comment