using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; using System.IO; using Microsoft.Office.Interop.Excel; using System.Globalization; namespace projectforratlamandoffice { public partial class Form1 : Form { string selectedFile; int iRow, iCol = 1; object[,] valueArray; List list = new List(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } void OpenKeywordsFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e) { OpenFileDialog fileDialog = sender as OpenFileDialog; selectedFile = System.IO.Path.GetFileNameWithoutExtension(fileDialog.FileName); if (string.IsNullOrEmpty(selectedFile) || selectedFile.Contains(".lnk")) { MessageBox.Show("Please select a valid Excel File"); e.Cancel = true; } return; } private void button1_Click(object sender, EventArgs e) { OpenFileDialog choofdlog = new OpenFileDialog(); choofdlog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); choofdlog.Multiselect = false; choofdlog.ValidateNames = true; choofdlog.DereferenceLinks = false; // Will return .lnk in shortcuts. choofdlog.Filter = "Excel |*.xlsx"; choofdlog.FilterIndex = 1; choofdlog.FileOk += new System.ComponentModel.CancelEventHandler(OpenKeywordsFileDialog_FileOk); if (choofdlog.ShowDialog() == DialogResult.OK) { selectedFile = choofdlog.FileName; } // Set cursor as hourglass Cursor.Current = Cursors.WaitCursor; dowork(); // Set cursor as default arrow Cursor.Current = Cursors.Default; } public static Excel.Workbook Open(Excel.Application excelInstance, string fileName, bool readOnly = false, bool editable = true, bool updateLinks = true) { Excel.Workbook book = excelInstance.Workbooks.Open( fileName, updateLinks, readOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); return book; } void dowork() { Excel.Application excel = null; excel = new Excel.Application(); excel.Visible = true; Excel.Workbook wkb = null; wkb = Open(excel, selectedFile); Excel._Worksheet sheet = wkb.Sheets[1]; sheet.Range["B1"].EntireColumn.NumberFormat = @"[>=10000000]##\,##\,##\,##0;[>=100000] ##\,##\,##0;##,##0.00"; sheet.Range["C1"].EntireColumn.NumberFormat = @"[>=10000000]##\,##\,##\,##0;[>=100000] ##\,##\,##0;##,##0.00"; Excel.Range range1 = sheet.UsedRange; var arr1 = (object[,])range1.get_Value(XlRangeValueDataType.xlRangeValueDefault); List listnew = new List(); for (int i = 10; i <= arr1.GetLength(0) - 1; i++) { if (arr1[i, 1] != null) { if (arr1[i, 2] != null && arr1[i, 3] == null) { listnew.Add(new object[] { arr1[i, 1], "+ " + arr1[i, 2] + " ₹" }); } else if (arr1[i, 2] == null && arr1[i, 3] != null) { listnew.Add(new object[] { arr1[i, 1], "- " + arr1[i, 3] + " ₹" }); } } }