Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static string GetColumnLetter(int columnNumber)
- {
- var dividend = columnNumber;
- var columnName = String.Empty;
- while (dividend > 0)
- {
- var modulo = (dividend - 1) % 26;
- columnName = Convert.ToChar(65 + modulo) + columnName;
- dividend = ((dividend - modulo) / 26);
- }
- return columnName;
- }
- // Insert into Excel
- public static void ExcelInsert(string mFunction, string mColor, int mQty, string mFau, string mPrice)
- {
- var values = new List<string>
- {
- mFunction,
- mColor,
- mQty.ToString(),
- mFau,
- mPrice
- }.ToArray();
- var rowNumber = Globals.ThisAddIn.Application.ActiveCell.Row;
- var columnNumber = Globals.ThisAddIn.Application.ActiveCell.Column;
- var columnLetter = GetColumnLetter(columnNumber);
- for (var i = 0; i < values.Count(); i++ )
- {
- var range = Globals.ThisAddIn.Application.Range[String.Format("{0}{1}", columnLetter, rowNumber)];
- range.Value = values[i];
- columnNumber++;
- columnLetter = GetColumnLetter(columnNumber);
- }
- }
- using Excel = Microsoft.Office.Interop.Excel;
- void MyMethod()
- {
- //Replace '7' with the number of fields on your Windows Form
- int numberOfFields = 7;
- string[] array = new string[numberOfFields];
- array[0] = textBoxOneValue;
- array[1] = textBoxTwoValue;
- array[2] = textBoxThreeValue;
- array[3] = textBoxFourValue;
- array[4] = textBoxFiveValue;
- array[5] = textBoxSixValue;
- array[6] = textBoxSevenValue;
- Excel.Application application = new Excel.Application();
- Excel.Workbook workbook = application.Workbooks.Open(@"C:whatever.xlsx");
- Excel.Worksheet worksheet = workbook.ActiveSheet;
- Excel.Range activeCell = application.ActiveCell;
- int rowNumber = activeCell.Row;
- int columnNumber = activeCell.Column;
- string columnLetter = GetColumnLetter(columnNumber);
- for(int i = 0; i < numberOfFields; i++)
- {
- Excel.Range range = worksheet.get_Range(String.Format("{0}{1}", columnLetter, rowNumber));
- range.Value = array[i];
- columnNumber++;
- columnLetter = GetColumnLetter(columnNumber);
- }
- }
- string GetColumnLetter()
- {
- int dividend = columnNumber;
- string columnName = String.Empty;
- int modulo;
- while (dividend > 0)
- {
- modulo = (dividend - 1) % 26;
- columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
- dividend = (int)((dividend - modulo) / 26);
- }
- return columnName;
- }
- ActiveCell.Resize(, UBound(arr) - LBound(arr) + 1).Value = arr
- Dim x(3) As String ' your array
- x(0) = "abc"
- x(1) = "def"
- x(2) = "ghi"
- x(3) = "jkl"
- Dim r As Range ' range to paste
- Set r = ActiveCell
- For Each s In x
- r.Value = s
- Set r = r.Offset(,1) ' keep going right
- Next
- Dim x(4) As String ' this can actually contain 5 items, from 0 to 4
- x(1) = "abc"
- x(2) = "def"
- x(3) = "ghi"
- x(4) = "jkl"
- Dim r As Range ' range to paste
- Set r = ActiveCell
- For i = 1 To UBound(x) ' skip 0
- r.Value = x(i)
- Set r = r.Offset(, 1) ' keep going right
- Next
Add Comment
Please, Sign In to add comment