S
Spitfyre586
Guest
I am writing a program in C# that reads and randomly populates an excel spreadsheet with various types of cells including drop-downs (the workbooks are all different and I never know where they are going to put the drop down. I don't really care what data is in the drop-down I just need to select an item from the list and populate the cell). I am using Microsoft.Office.Interop.Excel and have functionality working for the most part but am struggling on how to locate the drop downs and then populate with the default or any value in the drop-down list. My code is as follows. (be kind I am still new to c# )
Thank you in advance for any assistance you can provide.
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
Excel.DropDowns xlDropDowns;
Excel.DropDown xlDropDown;
string path = @"folder path foes here"; //will need to use path once folders are set up permanently
string outputPath = @"folder path goes here"
string fn = "";
string str;
Random random = new Random(100);
int rand = random.Next(100, 5000);
DirectoryInfo dI = new DirectoryInfo(@"path goes here");//will need to use path once folders are set up permanently
FileInfo[] fileName = dI.GetFiles("*.xls*");
foreach (FileInfo file in fileName)
{
fn = file.Name;
//Console.WriteLine("GetFilename = " + fn);
}
xlApp = new Excel.Application();
completedLabel.Text = "working";
xlWorkBook = xlApp.Workbooks.Open(@"path goes here" + fn, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // this is getting 1 - how to get more?
int rCnt;
int cCnt;
int wsCount = xlWorkBook.Worksheets.Count;
Console.WriteLine("# ws: " + wsCount);
range = xlWorkBook.ActiveSheet.UsedRange; // this is to find the 'range' of used cells in the worksheet
xlDropDowns = ((Excel.DropDowns)(xlWorkBook.ActiveSheet.DropDowns(Type.Missing)));
<g class="gr_ gr_736 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="736" id="736">foreach</g> (Excel<g class="gr_ gr_738 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" data-gr-id="738" id="738">.Worksheet</g> <g class="gr_ gr_737 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="737" id="737">xlworksheet</g> in xlWorkBook.Worksheets)
{
int cellCnt = 0;
int rw = 0;
int cl = 0;
rw = range.Rows.Count;
cl = range.Columns.Count;
for (rCnt = 1; rCnt <= rw; rCnt++) // loop through row
{
for (cCnt = 1; cCnt <= cl; cCnt++) //loop though column
{
// **** TODO **********************************
//if (is a dropdownlist cell) //if (default exists) // use default //else //use list<1>
//***********************************************
str = Convert.ToString((range.Cells[rCnt, cCnt] as Excel.Range).Value2);
if (range.Cells[rCnt, cCnt].Locked == false && string.IsNullOrEmpty(str) && !range.Cells[rCnt, cCnt].HasFormula)
//True if the object is locked; False if the object can be modified when the sheet is protected. Returns Null if the specified range contains both locked and unlocked cells.
{
range.Cells[rCnt, cCnt] = rand.ToString();
rand++;
cellCnt++;
}
}
rowCountLabel.Text = "Row Count is : " + rCnt;
colCountLabel.Text = "Column Count is : " + cCnt;
cellCountLabel.Text = "Unlocked Cell Count is : " + cellCnt; // add in a cell count
}
completedLabel.Text = "completed";
xlWorkBook.SaveAs(outputPath + fn); // Save workbook to output file
xlWorkBook.Close(true, null, null);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}
private void closeButton_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
}
Continue reading...
Thank you in advance for any assistance you can provide.
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
Excel.DropDowns xlDropDowns;
Excel.DropDown xlDropDown;
string path = @"folder path foes here"; //will need to use path once folders are set up permanently
string outputPath = @"folder path goes here"
string fn = "";
string str;
Random random = new Random(100);
int rand = random.Next(100, 5000);
DirectoryInfo dI = new DirectoryInfo(@"path goes here");//will need to use path once folders are set up permanently
FileInfo[] fileName = dI.GetFiles("*.xls*");
foreach (FileInfo file in fileName)
{
fn = file.Name;
//Console.WriteLine("GetFilename = " + fn);
}
xlApp = new Excel.Application();
completedLabel.Text = "working";
xlWorkBook = xlApp.Workbooks.Open(@"path goes here" + fn, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // this is getting 1 - how to get more?
int rCnt;
int cCnt;
int wsCount = xlWorkBook.Worksheets.Count;
Console.WriteLine("# ws: " + wsCount);
range = xlWorkBook.ActiveSheet.UsedRange; // this is to find the 'range' of used cells in the worksheet
xlDropDowns = ((Excel.DropDowns)(xlWorkBook.ActiveSheet.DropDowns(Type.Missing)));
<g class="gr_ gr_736 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="736" id="736">foreach</g> (Excel<g class="gr_ gr_738 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" data-gr-id="738" id="738">.Worksheet</g> <g class="gr_ gr_737 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="737" id="737">xlworksheet</g> in xlWorkBook.Worksheets)
{
int cellCnt = 0;
int rw = 0;
int cl = 0;
rw = range.Rows.Count;
cl = range.Columns.Count;
for (rCnt = 1; rCnt <= rw; rCnt++) // loop through row
{
for (cCnt = 1; cCnt <= cl; cCnt++) //loop though column
{
// **** TODO **********************************
//if (is a dropdownlist cell) //if (default exists) // use default //else //use list<1>
//***********************************************
str = Convert.ToString((range.Cells[rCnt, cCnt] as Excel.Range).Value2);
if (range.Cells[rCnt, cCnt].Locked == false && string.IsNullOrEmpty(str) && !range.Cells[rCnt, cCnt].HasFormula)
//True if the object is locked; False if the object can be modified when the sheet is protected. Returns Null if the specified range contains both locked and unlocked cells.
{
range.Cells[rCnt, cCnt] = rand.ToString();
rand++;
cellCnt++;
}
}
rowCountLabel.Text = "Row Count is : " + rCnt;
colCountLabel.Text = "Column Count is : " + cCnt;
cellCountLabel.Text = "Unlocked Cell Count is : " + cellCnt; // add in a cell count
}
completedLabel.Text = "completed";
xlWorkBook.SaveAs(outputPath + fn); // Save workbook to output file
xlWorkBook.Close(true, null, null);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}
private void closeButton_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
}
Continue reading...