Create a dropdownlist of checkboxes using EPPlus - c#

I am using EPPlus (http://epplus.codeplex.com/) to create Excel files in my ASP.NET application. I was wondering if anyone knows if it's possible to create a dropdownlist with checkboxes in one cell. I've checked the documentation, but found nothing so far. I've also tried Googling it but haven't found anything in the right direction yet.
This question on their forum is actually a pretty good demonstration of what I'm looking for, but it hasn't received any answers: http://epplus.codeplex.com/discussions/585879
Does anyone happen to have any ideas and can point me in the right direction?

You can use:
var validationCell = sheet.DataValidations.AddListValidation("A1");
validationCell.Formula.Values.Add("a");
validationCell.Formula.Values.Add("b");
validationCell.Formula.Values.Add("c");
...
but you can choose only one single value. I think that multiple values are not supported by excel.

For others reference, here is how I could enable multiple values select:
By inserting VBA Code:
see how to do it in here , using EPPlus you can insert VBA code using something like:
package.Workbook.CreateVBAProject();
worksheet.CodeModule.Code = code;
This following code did the trick:
using (var package = new ExcelPackage(new System.IO.FileInfo("D:\\b.xlsm")))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("11");
var validationCell = worksheet.DataValidations.AddListValidation("A1");
validationCell.Formula.Values.Add("a");
validationCell.Formula.Values.Add("b");
validationCell.Formula.Values.Add("c");
string code = "Private Sub Worksheet_Change(ByVal Target As Range)\n" +
"Dim Oldvalue As String\n" +
"Dim Newvalue As String\n" +
"Application.EnableEvents = True\n" +
"On Error GoTo Exitsub\n" +
"If Target.Address = \"$A$1\" Then\n" +
" If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then\n" +
" GoTo Exitsub\n" +
"Else: If Target.Value = \"\" Then GoTo Exitsub Else\n" +
" Application.EnableEvents = False\n" +
"Newvalue = Target.Value\n" +
"Application.Undo\n" +
"Oldvalue = Target.Value\n" +
" If Oldvalue = \"\" Then\n" +
" Target.Value = Newvalue\n" +
"Else\n" +
" If InStr(1, Oldvalue, Newvalue) = 0 Then\n" +
" Target.Value = Oldvalue & \", \" & Newvalue\n" +
"Else: \n" +
" Target.Value = Oldvalue\n" +
"End If\n" +
"End If\n" +
"End If\n" +
"End If\n" +
"Application.EnableEvents = True\n" +
"Exitsub: \n" +
" Application.EnableEvents = True\n" +
"End Sub";
package.Workbook.CreateVBAProject();
worksheet.CodeModule.Code = code;
package.Save();
}

Related

EzAPI - SSIS Set EzOleDbDestination Error output configuration

I am creating a C# Program to generate a SSIS package, does anyone know how to set the ErrorOutput property of an EzOleDbDestination object to "Redirect Row"?
Edit :
EzOleDbDestination db_dest = new EzOleDbDestination(dataFlow)
{
Name = "Destination " + File_Name,
Connection = oldb_connection,
Table = "[dbo].[" + File_Name + "]"
};
EzOleDbDestination db_dest_clean_error = new EzOleDbDestination(dataFlow)
{
Name = "Destination " + File_Name + "_CleanError",
Connection = oldb_connection,
Table = "[dbo].[" + File_Name + "_CleanError]"
};
db_dest.AttachTo(file_source);
db_dest_clean_error.AttachTo(db_dest);
db_dest.LinkAllInputsToOutputs();
db_dest_clean_error.LinkAllInputsToOutputs();
dataFlow.AttachTo(Sql_Create);
package.SaveToFile("C:\\Users\\LGuerin\\Desktop\\Package_" + Engagement + ".dtsx"); ;
These two lines make it work, Thank you billinkc for your answers!

Output showing System.Windows.Form.Label after every word

I am having a problem where the user enters his inputs into a text box (like a name). If the name is Jim Bob it will output like Jim System.Windows.Form.Label Bob System.Windows.Form.Label. I searched the properties tab and I am following along from the book but they do not have the same problem I have. I added some code below but I don't think it has anything to do with the problem.
string output;
output = textBoxPrefT.Text + ". " + textBoxFirstN + " " + textBoxMiddleN + " " + textBoxLastN;
labelOutput.Text = output;
You have to include .Text for all textboxes so it should look like this
string output;
output = textBoxPrefT.Text + ". " + textBoxFirstN.Text + " " + textBoxMiddleN.Text + " " + textBoxLastN.Text;
labelOutput.Text = output;

C# excel range.FormulaArray not working

Here is my C# code to write ArrayFormula to certain cell in excel.
I am using UFT (Unified Functional Testing) which uses C# for custom code.
String sheetName = "xyz";
String wsMethodName = "abc";
int i = 2;
Excel.Application xlApp = null;
xlApp = new Excel.ApplicationClass();
wb = xlApp.Workbooks.Open(srcFile,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
worksheet = (Excel.Worksheet)wb.Worksheets[sheetName];
Excel.Range excelCell = (Excel.Range)worksheet.get_Range("B2", "B21");
foreach (Excel.Range c in excelCell)
{
//
strAvgFormula =
"=AVERAGEIFS(" +
"(OFFSET(\'" + sheetName + "\'!$A$1,2,2,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1))," +
"OFFSET(\'" + sheetName + "\'!$A$1,2,16382,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)," +
"(MID(C" + i + ",1,(FIND(\"-\",C" + i + "))-2))," +
"OFFSET(\'" + sheetName + "\'!$A$1,2,16383,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)," +
"(MID(C" + i + ",(FIND(\"-\",C" + i + ")+1),(FIND(\"/\",C" + i + "))-(FIND(\"-\",C" + i + ")+1))))";
this.CodeActivity16.Report("strAvgFormula",strAvgFormula);
//
strMaxFormula =
"=MAX(" +
"IF((OFFSET(\'" + sheetName + "\'!$A$1,2,16382,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)=MID(C" + i + ",1,(FIND(\"-\",C" + i + "))-2))*" +
"(OFFSET(\'" + sheetName + "\'!$A$1,2,16383,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)=MID(C" + i + ",(FIND(\"-\",C" + i + ")+2)," +
"(FIND(\"/\",C" + i + "))-(FIND(\"-\",C" + i + ")+2)))," +
"OFFSET(\'" + sheetName + "\'!$A$1,2,2,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)))";
this.CodeActivity16.Report("strMaxFormula",strMaxFormula);
if (c.Value2.ToString() == wsMethodName)
{
newExcelCell = (Excel.Range)worksheet.get_Range("F" + i, "F" + i);
newExcelCell.Clear();
newExcelCell.FormulaArray = strAvgFormula; //Failing # this line, error is mentioned below
//newExcelCell.Value = strAvgFormula;
newExcelCell = (Excel.Range)worksheet.get_Range("G" + i, "G" + i);
newExcelCell.Clear();
newExcelCell.FormulaArray = strMaxFormula;
//newExcelCell.Value = strMaxFormula;
break;
}
i ++;
}
wb.Save();
xlApp.Workbooks.Close();
xlApp.Quit();
releaseObject(newExcelCell);
releaseObject(excelCell);
releaseObject(worksheet);
releaseObject(wb);
releaseObject(xlApp);
private void releaseObject(object obj)
{
try
{
Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
CodeActivity16.Report("Error","Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
Now, If I copy the same formula from printed output result and paste it in my desired cell, it is working fine.
Escape characters are doing their job properly.
If I change newExcelCell.FormulaArray to newExcelCell.Value, than it is writing to excel but it works as normal formula and not the ArrayFormula (like we do Ctrl + Shift + Enter).
Here is the error that I am getting from result file:
The formula you typed contains an error. Try one of the following:
• Make sure you've included all parentheses and required arguments.
• To get assistance with using a function, click Function Wizard on the Formulas tab (in the Function Library group).
• If you include a reference to another sheet or workbook, verify that the reference is correct.
• If you are not trying to enter a formula, avoid using an equal sign (=) or minus sign (-), or precede it with a single quotation mark ( ' ).
• For more information about common formula problems, click Help.
Thanks in advance for any help or suggestion.
UPDATE:
Here is the formula for strAvgFormula that I am trying to write.
"=AVERAGEIFS(" +
"(OFFSET('1'!$A$1,2,2,COUNTA('1'!$A:$A)-2,1))," +
"OFFSET('1'!$A$1,2,16382,COUNTA('1'!$A:$A)-2,1),(MID(C2,1,(FIND("-",C2))-2))," +
"OFFSET('1'!$A$1,2,16383,COUNTA('1'!$A:$A)-2,1),(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))"
Formula for strMaxFormula is working fine.
The FormulaArray property also has a character limit of 255.
A workaround can be to set it to less than 256 characters, and replace part of it. For example:
range.FormulaArray = "={1,2,3}";
range.Replace("}", ",4,5,6}", XlLookAt.xlPart);
https://colinlegg.wordpress.com/2012/05/23/working-with-range-formulaarray-in-vba/
http://dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/
So, finally I got the workaround for this issue.
It turned out (this is what I feel) that either the Excel is not accepting the formula from the string strAvgFormula or it get messed up because of cell reference using Offset.
I've to use direct cell reference.
Here is my working formula:
int lastUsedRowDiffSheet = worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,Type.Missing).Row;
strAvgFormula =
"=AVERAGEIFS('" + sheetName + "\'!C" + iRowCount + ":C" + lastUsedRowDiffSheet + ","
+ "'" + sheetName + "'!XFC" + iRowCount + ":XFC" + lastUsedRowDiffSheet + ","
+ "MID(C" + i + ",1,(FIND(\"-\",C" + i + "))-2),"
+ "'" + sheetName + "\'!XFD" + iRowCount + ":XFD" + lastUsedRowDiffSheet + ","
+ "(MID(C" + i + ",(FIND(\"-\",C" + i + ")+1),(FIND(\"/\",C" + i + "))-(FIND(\"-\",C" + i + ")+1))))";

Disable Save functionality in Excel with VBA through C#

I got one function where I need to protect the Excel file uploaded in server where it can't be edited or even saved.
To achieve this, I programmatically secure the file and added some of VBA code limit what user can do with it.
The VBA code function suppose to have 2 functionality where it can hide the Ribbon and prevent Save/SaveAs function.
You may refer into my code where I got 3 version of VBA code inside but the most desired is the no.3 of VBA code inside it.
This is my function for that purpose where I will pass the file location in string to this function.
I wish someone can assist me on how to allow the program to save the secured Excel file (using VBA code no.3).
From what I understand, I can't save it due to the VBA code on Save(not SaveAs) part won't allow this line wbkExcel.Save() to proced.
You may test the VBA code in your Excel file see if it's working or not (tested working for me so far...)
Thanks in advance everyone...
protected void ExcelEncryptor(string strExcelFile)
{
Microsoft.Office.Interop.Excel.Application wAppExcel = new Microsoft.Office.Interop.Excel.Application();
wAppExcel.Interactive = false;
wAppExcel.Visible = true;
wAppExcel.DisplayAlerts = false;
wAppExcel.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
Microsoft.Office.Interop.Excel.Workbook wbkExcel = wAppExcel.Workbooks.Open(strExcelFile.ToString(),System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value);
string strVBCode = string.Empty;
//To hide Ribbon only
/*strVBCode = "Private Sub Workbook_Open()\r\n" +
" msgbox \"This document is protected!\"\r\n" +
" application.ExecuteExcel4Macro \"show.toolbar(\"\"Ribbon\"\",False)\"\r\n" +
"End Sub";*/
//To hide Ribbon + Disable SaveAs (F12 key) but still can Save (Ctrl+S key)
/*strVBCode = "Private Sub Workbook_Open()\r\n" +
" msgbox \"This document is protected!\"\r\n" +
" application.ExecuteExcel4Macro \"show.toolbar(\"\"Ribbon\"\",False)\"\r\n" +
"End Sub\r\n" +
"Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)\r\n" +
" If (SaveAsUI = True) Then\r\n" +
" msgbox \"You are not allowed to save this document!\"\r\n" +
" Cancel = True\r\n" +
" End If\r\n" +
"End Sub";*/
//To hide Ribbon + Disable SaveAs (F12 key) and cannot Save (Ctrl+S key)
strVBCode = "Option Explicit\r\n" +
"Dim SaveByCode As Boolean\r\n" +
"Const msg As String = \"You are not allowed to save this document!\"\r\n" +
"Const ttl As String = \"This document is protected!\"\r\n" +
"Private Sub Workbook_Open()\r\n" +
" MsgBox msg, vbExclamation, ttl\r\n" +
" application.ExecuteExcel4Macro \"show.toolbar(\"\"Ribbon\"\",False)\"\r\n" +
"End Sub\r\n" +
"Private Sub Workook_BeforeClose(Cancel As Boolean)\r\n" +
" If Me.Saved = False And SaveByCode = False Then\r\n" +
" MsgBox msg, vbExclamation, ttl\r\n" +
" Cancel = True\r\n" +
" End If\r\n" +
"End Sub\r\n" +
"Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)\r\n" +
" Application.EnableEvents = False\r\n" +
" If SaveByCode = True Then\r\n" +
" SaveThisFile\r\n" +
" Else\r\n" +
" MsgBox msg, vbExclamation, ttl\r\n" +
" Cancel = True\r\n" +
" End If\r\n" +
" Application.EnableEvents = True\r\n" +
"End Sub\r\n" +
"Sub SaveThisFile()\r\n" +
" SaveByCode = True\r\n" +
" ThisWorkbook.Save\r\n" +
"End Sub";
Microsoft.Vbe.Interop.VBProject vbMacro = wbkExcel.VBProject;
Microsoft.Vbe.Interop.VBComponent vbCode = vbMacro.VBComponents.Item("ThisWorkBook");
Microsoft.Vbe.Interop.CodeModule vbModule = vbCode.CodeModule;
vbModule.AddFromString(strVBCode.ToString());
wbkExcel.Protect("Pa$$w0rd!", true, false);
foreach (Microsoft.Office.Interop.Excel.Worksheet wstExcel in wAppExcel.Worksheets)
{
wstExcel.Protect("Pa$$w0rd!", true, true, true, true, false, false, false, false, false, false, false, false, true, true, false);
}
wbkExcel.Save();
wbkExcel.Close(System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value);
Marshal.ReleaseComObject(wbkExcel);
Marshal.ReleaseComObject(wAppExcel);
GC.Collect();
}
After a lot of try & error of codes, I finally manage to achieve what I need, the Excel file will be protected+cannot press F12 key+cannot press Ctrl+S key+cannot press Ctrl+P key anymore, the updated VBA code also can prevent user from opening Visual Basic editor if they wish to disable the code but in my case I leave it accessible for now. Refer my updated VBA code below :
//Excel file will be password protected+cannot press F12 key+cannot press Ctrl+S key+cannot press Ctrl+P key anymore
strVBCode = "Private Sub Workbook_Open()\r\n" +
" msgbox \"This document is protected!\"\r\n" +
" application.ExecuteExcel4Macro \"show.toolbar(\"\"Ribbon\"\",false)\"\r\n" +
" ThisWorkbook.Saved = True\r\n" +
" Application.OnKey \"^s\", \"\"\r\n" +
" Application.OnKey \"^p\", \"\"\r\n" +
//Enable this line if you wish to block Visual Basic Editor
//" Application.OnKey \"%{F11}\", \"\"\r\n" +
"End Sub\r\n" +
"Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)\r\n" +
" If (SaveAsUI = true) Then\r\n" +
" msgbox \"You are not allowed to save this document!\"\r\n" +
" Cancel = True\r\n" +
" Else\r\n" +
" ThisWorkbook.Saved = True\r\n" +
" Application.OnKey \"^s\", \"\"\r\n" +
" Application.OnKey \"^p\", \"\"\r\n" +
//Enable this line if you wish to block Visual Basic Editor
//" Application.OnKey \"%{F11}\", \"\"\r\n" +
" End If\r\n" +
"End Sub";
What I notice from my previous code, command(VBA code) Cancel=True in Private Sub Workbook_BeforeSave will prevent command(C#) wbkExcel.Save() from running because the VBA code will return false for C# code in save process. I hope someone can help and correct me if I'm wrong here. Hope this one will help the others too. Thanks StackOverflow.com.

c# replace timestamp value string of text in textfield

i currently have a textbox field, everytime a change is made, i add who it was updated by and what time
right now it keep appending that text
how can i find the line that says "LastEdited: ", and only replace the time stamp value?
this is what i do now
maybe someone can post me example of code how to grab the timestamp and replace it?
if (txtMemberNotesOriginally != txtMemberNotesChanged) {
txtMemberNotes.AppendText("LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
i am not an expert, so an example of code would bevery useful
i do use this textbox to save other notes as well (which i wouldn't wanna loose)
can i do this
foreach (var line in txtMemberNotes.Lines) {
if (line.StartsWith("Last Edited: "))
{
txtMemberNotes.Text = txtMemberNotes.Text.Replace(line, "Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
}
else
{
txtMemberNotes.AppendText("Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
}
}
The easiest way to solve your problem seems to stop appending text and just set the text value. No parsing is necessary, just overwrite the exiting values with your newer value.
if (txtMemberNotesOriginally != txtMemberNotesChanged) {
txtMemberNotes.Text = "LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine;
I guess theres always Regex if you really wanted:
string s = #"Some notes here etc etc..
Last edited: 12/12/2012 12:00:00 AM";
Console.Write(Regex.Replace(s, #"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString()));
Or more specifically using your code:
txtMemberNotes.Text = Regex.Replace(txtMemberNotes.Text, #"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString());

Categories

Resources