Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. private void exportBtn_Click(object sender, EventArgs e)
  2. {
  3. OleDbCommand newRow = new OleDbCommand();
  4. newRow.CommandType = CommandType.Text;
  5. newRow.CommandText = "INSERT INTO [Sheet1$] ([Company:], [County:], [State:], [Notes:], [User:], [Email:], [Username:], [Password:], [SMMM PWD:], [CAD:]) VALUES (1,2,3,4,5,6,7,8,9,10)";
  6. newRow.Connection = connectionExl;
  7.  
  8. connectionExl.Open();
  9. newRow.ExecuteNonQuery();
  10. connectionExl.Close();
  11. }
  12.  
  13. public int getLastRow(Excel.Worksheet ws)
  14. {
  15. object[,] arData = ws.UsedRange.FormulaR1C1; // FormulaR1C1 returns only string in 2 dimensional array
  16. for (int iRow = arData.GetUpperBound(0); iRow > 0; iRow--) {
  17. for (int iCol = 1; iCol <= arData.GetUpperBound(1); iCol++) {
  18. if (arData[iRow, iCol].ToString() != "") { return iRow; }
  19. }
  20. }
  21. return 0;
  22. }
  23.  
  24. int lastRow = ws.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
  25.  
  26. using System.Runtime.InteropServices;
  27. using Excel = Microsoft.Office.Interop.Excel;
  28.  
  29. void test()
  30. {
  31. Excel.Application xlApp = csXL.StartExcel();
  32. Excel.Worksheet ws = xlApp.ActiveSheet;
  33. int irow = getLastRow(ws);
  34. }
  35.  
  36. public static Excel.Application StartExcel()
  37. {
  38. Excel.Application instance = null;
  39. try { instance = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); }
  40. catch (System.Runtime.InteropServices.COMException ex) { instance = new Excel.Application(); }
  41. return instance;
  42. }
  43.  
  44. object[,] arLoad = new object[0, 9]; arLoad[0, 0] = 1; arLoad[0, 1] = 2;
  45. ws.Range["A" + irow + ":J" + irow].FormulaR1C1 = arLoad; // or use Value instead of FormulaR1C1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement