I have been looking all over for a solution to my problem but have not been able to find one.
I'm relatively new to C# and Winforms so excuse any mishaps please.
Info
I'm coding with C# and WinForms using Visual Studio 2022 version. I installed the SyncFusion.XlsIO.WinForms
NuGet Package to integrate my code with Excel. I have successfully been able to link my current form and its code to an Excel workbook and have managed to successfully transfer 'strings' that the user inputs on the WinForm to the Excel workbook into a specific cell I choose.
Problem
My goal is to put new user inputted information into the next available blank cell in a specific column of my choosing. So for example the first input the user submits (via a button event and textbox) to the workbook via the Winforms is "Hello", this automatically gets put into cell A3 (Cell A1 & A2 have headings). Then the user decides to submit a new piece of information, "World". I want "World" to then be placed into cell A4 automatically. Next input cell A5, thereafter cell A6, so on and so forth. Basically each click of the button, should store the inputted textbox string to the next available blank cell.
What I've tried/found
I have not been able to find any information regarding something that can help. Some pieces of code I found went straight over my head because they are too complex for me to understand and I have no idea how to integrate it into my code.
Something I did find on the SyncFusion Forums, what I suppose is a 'Mod' or 'developer' replied to a similar question saying that there is not built-function at the moment to find the next available blank cell in XlsIO (reply was April 2021). But I'm sure that means it can still be done just in a different way.
My code
This is currently all the Winforms -> Excel related code I have. NOTE, none of the code I have here attempts to find the next blank cell and store user input there. Currently I just take user input and store it in a specific cell if new user input < old user input. That is intentional.
using (ExcelEngine excelEngine = new ExcelEngine())
{
// Excel Setup
// Instantiate the Excel application object
IApplication application = excelEngine.Excel;
// Set the default application version
application.DefaultVersion = ExcelVersion.Xlsx;
// Load the existing Excel workbook into IWorkbook
IWorkbook workbook = excelEngine.Excel.Workbooks.Open(@"C:\Users\steph\Documents\Programming\C#\Self\Playground\GolfScoreDatabase\GolfDatabase.xlsx");
// Get the first worksheet in the workbook into IWorksheet
IWorksheet worksheet = workbook.Worksheets[0];
// Inputting and Fetching from Excel
// Compare inputted score to lowest score
// Contingency if the lowest score values are blank within the excel worksheet
if (worksheet.Range["E2"].Value == "" || worksheet.Range["E5"].Value == "")
{
worksheet.Range["E2"].Value = 150.ToString(); //150 is the value because logically its unobtainable in a round of golf
worksheet.Range["E5"].Value = 150.ToString();
}
// Fetch 18 hole low score & venue from excel
lowScoreFull = Convert.ToInt32(worksheet.Range["E2"].Value);
currentVenueFull = worksheet.Range["F2"].Value;
// Fetch 9 hole low score & venue from excel
lowScoreNine = Convert.ToInt32(worksheet.Range["E5"].Value);
currentVenueNine = worksheet.Range["F5"].Text;
// Compare inputted scores to previous high scores & paste into spreadsheet if conditions met
if (totalScore < lowScoreFull)
{
lowScoreFull = totalScore;
currentVenueFull = enteredVenue;
worksheet.Range["E2"].Text = totalScore.ToString();
worksheet.Range["F2"].Text = currentVenueFull.ToString();
}
if (tempNineScore < lowScoreNine)
{
lowScoreNine = tempNineScore;
currentVenueNine = enteredVenue;
worksheet.Range["E5"].Text = lowScoreNine.ToString();
worksheet.Range["F5"].Text = currentVenueNine.ToString();
worksheet.Range["G5"].Text = whichNine.ToString();
}
else
{
MessageBox.Show("There was no new lowest scores for 9 or 18 holes");
}
// Save the Excel document
workbook.SaveAs(@"C:\Users\steph\Documents\Programming\C#\Self\Playground\GolfScoreDatabase\GolfDatabase.xlsx");
}
CodePudding user response:
We have prepared the sample to store the user input to the next blank cell of the Excel. The complete sample can be downloaded from the following link.
Sample link - https://www.syncfusion.com/downloads/support/directtrac/general/ze/T-399594391912302
Regards, Ramya.