Export to excel functionality on JQuery dialoge buttons - MVC4 - asp.net-mvc-4

I have a Jquery dialoge box which contains 4 buttons . Export to excel is one of them.
I am trying to export my grid data to excel . Please let me know if you have any similar examples.
Please let me know , how should i write and call export to excel on jquery dialog button click.

To write something to excel file:
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
object misValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Workbook workBook = excelApp.Workbooks.Add(misValue);
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range range;
//top of document
workSheet.Cells[1, 1] = string.Format("{0:dd.MM.yyyy}", DateTime.Now);
workSheet.Cells[1, 6] = "Something1";
workSheet.Cells[2, 3] = "Something2";
workSheet.Cells[4, 3] = "Something3";
workSheet.Cells[6, 3] = "Something4";
// test content
for (int i = 0; i < 10; i++)
{
workSheet.Cells[i + 9, 1] = (i + 1).ToString();
workSheet.Cells[i + 9, 2] = i.ToString();
workSheet.Cells[i + 9, 3] = Math.Round(2.555, 2);
workSheet.Cells[i + 9, 4] = "Hi";
workSheet.Cells[i + 9, 5] = i.ToString();
workSheet.Cells[i + 9, 6] = i.ToString();
}
//show document
excelApp.Visible = true;
When "Export to excell" button clicks, call a action that contains above code.

Related

EPPlus: Customizing clustered bar chart data label text

I have created a clustered bar chart with EPPlus as shown here:
I would like to customize each bar label to include the percentage, as pictured here:
How can I customize the label? I didn't see any ability to do this via the DataLabel.
Here is the code used to create the data and chart:
var startCell = worksheet.Cells[1, 1];
startCell.Offset(0, 0).Value = "Theme";
startCell.Offset(0, 1).Value = "Count";
startCell.Offset(0, 2).Value = "%";
startCell.Offset(1, 0).Value = "Working Remotely";
startCell.Offset(1, 1).Value = 53;
startCell.Offset(1, 2).Value = 0.11;
startCell.Offset(2, 0).Value = "Collaboration";
startCell.Offset(2, 1).Value = 42;
startCell.Offset(2, 2).Value = 0.05;
worksheet.Cells[FromRow: 2, FromCol: 3, ToRow: 3, ToCol: 3].Style.Numberformat.Format = "0%";
worksheet.Cells[FromRow: 1, FromCol: 1, ToRow: 3, ToCol: 3].AutoFitColumns();
var barChart = (ExcelBarChart) worksheet.Drawings.AddChart("ThemeBarChart", eChartType.BarClustered);
barChart.SetPosition(4, 0, 0, 0);
barChart.SetSize(500, 150);
barChart.Series.Add(worksheet.Cells[FromRow: 2, FromCol: 2, ToRow: 3, ToCol: 2], worksheet.Cells[FromRow: 2, FromCol: 1, ToRow: 3, ToCol: 1]);
barChart.Legend.Remove();
var xAxis = barChart.XAxis;
xAxis.MajorTickMark = eAxisTickMark.None;
xAxis.MinorTickMark = eAxisTickMark.None;
xAxis.Fill.Style = eFillStyle.NoFill;
var yAxis = barChart.YAxis;
yAxis.Deleted = true;
yAxis.RemoveGridlines();
var dataLabel = barChart.Series[0].DataLabel;
dataLabel.Position = eLabelPosition.OutEnd;
dataLabel.ShowValue = true;

Is there a way to sort a list of numbers whilst maintaining their original indexes in VB.net?

I need to sort a list of numbers but I need to keep their initial indexes.
I had previously just created an array of the numbers and then another array of the indexes which I sorted at the same time like so:
Dim AverageSuccess(23) As Decimal
Dim intervalList() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}
x = 2
Do
Sorted = True
For i = 23 To x Step -1
If AverageSuccess(i) < AverageSuccess(i - 1) Then
TempNum = AverageSuccess(i)
AverageSuccess(i) = AverageSuccess(i - 1)
AverageSuccess(i - 1) = TempNum
TempIndex = intervalList(i)
intervalList(i) = intervalList(i - 1)
intervalList(i - 1) = TempIndex
Sorted = False
End If
Next
x += 1
Loop Until Sorted
however this is for a project and my teacher informed me that this is bad programming practise and I should be using a list instead.
I have struggled to find a simple example of how to use a list in VB.net for this purpose, so if someone could give me an example I would appreciate it.
I don't know how much you have covered about lists in class...
Let us create a list with some integers in:
Dim nums As New List(Of Integer) From {9, 8, 4, 5}
Now, we want to store the original indices of those numbers. We can do that with the Select method, which has an optional parameter that will give the index of the current item, and create a new entity with items which we can give names to, say "Num" and "Idx":
Dim numsWithIndex = nums.Select(Function(n, i) New With {.Num = n, .Idx = i})
Then we can use the LINQ method OrderBy to get those entities in the desired order:
Dim sortedNums = numsWithIndex.OrderBy(Function(nwi) nwi.Num)
And we can have a look at what we have constructed with
Console.WriteLine(String.Join(vbCrLf, sortedNums))
which outputs:
{ Num = 4, Idx = 2 }
{ Num = 5, Idx = 3 }
{ Num = 8, Idx = 1 }
{ Num = 9, Idx = 0 }
(It shows the names we gave to the properties of the anonymous type created with New earlier.)
Here is the whole thing as a console app you can copy-and-paste to investigate with on your computer:
Module Module1
Sub Main()
Dim nums As New List(Of Integer) From {9, 8, 4, 5}
Dim numsWithIndex = nums.Select(Function(n, i) New With {.Num = n, .Idx = i})
Dim sortedNums = numsWithIndex.OrderBy(Function(nwi) nwi.Num)
Console.WriteLine(String.Join(vbCrLf, sortedNums))
Console.ReadLine()
End Sub
End Module

How to change the style of the excel chart in vsto word addin?

how to change the style and how make x axis and y axis bold with differnt colour?
Word.Chart wdChart = doc.InlineShapes.AddChart(Microsoft.Office.Core.XlChartType.xlColumnClustered, ref missing).Chart;
wdChart.Legend.LegendEntries(wdChart.Legend.LegendEntries().Count).Delete();
wdChart.Legend.Clear();
wdChart.ChartStyle = 311;
Word.ChartData chartData = wdChart.ChartData;
Excel.Workbook dataWorkbook = (Excel.Workbook)chartData.Workbook;
Excel.Worksheet dataSheet = (Excel.Worksheet)dataWorkbook.Worksheets[1];
//wdChart.ChartTitle.Text = String.Empty;
dataWorkbook.Application.Visible = false;
for (int i = 0; i < xData.Count; i++)
{
((Excel.Range)dataSheet.Cells.get_Range("A" + (i + 2).ToString(), missing)).FormulaR1C1 = xData[i].ToString();
((Excel.Range)dataSheet.Cells.get_Range("B" + (i + 2).ToString(), missing)).FormulaR1C1 = yData[i].ToString();
((Excel.Range)dataSheet.Cells.get_Range("C" + (i + 2).ToString(), missing)).FormulaR1C1 = zData[i].ToString();
}
wdChart.HasTitle = false;
dataWorkbook.Application.Quit();

Cannot find column 25 Exception coming

I am creating one report where data exported from my access database to excel here. I'm not using any datagridview , when user click one checkbox appropriate report he can able to download for this I have written code but when I run this code its giving me Cannot find column 25, I don't know why its showing me this?
In access table I have total 25 column :
string sql = null;
string data = null;
int i = 0;
int j = 0;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
string connectionString = null;
connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
cnn.ConnectionString = connectionString;
cnn.Open();
sql = "SELECT * FROM Billing WHERE Bill_No and Bill_Date is null order by FormNo desc";
OleDbDataAdapter dscmd = new OleDbDataAdapter(sql, cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);
cnn.Close();
for (j = 0; i < ds.Tables[0].Columns.Count; j++)
{
xlWorkSheet.Cells[1, j + 1] = ds.Tables[0].Columns[j].Caption; // Exception coming this line
}
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (j = 0; j < ds.Tables[0].Columns.Count; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
}
}
System.Windows.Forms.SaveFileDialog saveDlg = new System.Windows.Forms.SaveFileDialog();
saveDlg.InitialDirectory = #"C:\";
saveDlg.Filter = "Excel files (*.xls)|*.xls";
saveDlg.FilterIndex = 0;
saveDlg.RestoreDirectory = true;
saveDlg.Title = "Export Excel File To";
xlWorkBook.Close(true, misValue, misValue);
MessageBox.Show("File Downloaded successfully...");
xlApp.Quit();
}
i got the answer here i change my for loop
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
xlWorkSheet.Cells[1, j + 1] = ds.Tables[0].Columns[j].Caption;
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
}
}

How to iterate a jagged array?

This has been driving me crazy for a few days. Why doesn't the following work?
Dim arr(3, 3) As Integer
For y As Integer = 0 To arr.GetLength(0) - 1
For x As Integer = 0 To arr.GetLength(y) - 1
arr(y, x) = y + x
Next
Next
Also, what if the array looked like this instead?
{ {1, 2, 3},
{4, 5, 6, 7, 8, 9, 9, 9},
{5, 4, 3, 2}
}
Because there is no '2' or '3' dimension. Should be .GetLength(1) instead of .GetLength(y)
Also: in VB.Net array declarations work a little differently. The subscript you specify in the declaration is the last index, not the number of items created like with C# or C++. But the array is still 0-indexed like C# or C++, instead of 1-indexed like VB6. That means that if you move to VB.Net from a different language your array instincts are probably wrong, no matter which language it is. In VB.Net, Dim arr(3,3) As Integer actually creates a 4x4 array.
Ok, so what you really need is a "jagged array". This will allow you to have an "array that contains other arrays of varying lengths".
Dim arr As Integer()() = {New Integer() {1, 2, 3}, New Integer() {4, 5, 6, 7, 8, 9, 9, 9}, New Integer() {5, 4, 3, 2}}
For x = 0 To arr.GetUpperBound(0)
Console.WriteLine("Row " & x & " has " & arr(x).GetUpperBound(0) & " columns")
For y = 0 To arr(x).GetUpperBound(0)
Console.WriteLine("(" & x & "," & y & ") = " & arr(x)(y))
Next
Next
Output:
Row 0 has 2 columns
(0,0) = 1
(0,1) = 2
(0,2) = 3
Row 1 has 7 columns
(1,0) = 4
(1,1) = 5
(1,2) = 6
(1,3) = 7
(1,4) = 8
(1,5) = 9
(1,6) = 9
(1,7) = 9
Row 2 has 3 columns
(2,0) = 5
(2,1) = 4
(2,2) = 3
(2,3) = 2
arr.GetLength(y)
should be
arr.GetLength(1)
Well what if I had an array that looked like this
{ {1, 2, 3},
{4, 5, 6, 7, 8, 9, 9, 9},
{5, 4, 3, 2}
}
How would GetLength(1) still know the length of each row?
Basically what I want is.... a way to find the number of elements in any given row.
Dim arr(3, 3) As Integer
Dim y As Integer
Dim x As Integer
For x = 0 To arr.Rank - 1
For y = 0 To arr.GetLength(x) - 2
arr(x, y) = x + y
Next
Next
The above code worked for me.
Edit, the code feels dirty though. I'm wondering what it is you are trying to accomplish?
Your declaration: DIM arr(3,3) As Integer allready specifies that there are 3 elements in any given row (or 4, I'm not so sure about VB)
You could try:
Dim arr(3) as Integer()
You should then be able to do:
arr(n).Length
To find the length of row n.
I'm a bit rusty on VB6 and never learned VB.NET, but this should give you a 'jagged' array. Check out the msdn documentation on multidimensioned arrays.
This code en C# is to get all the combinations of items in a jagged array:
static void Main(string[] args)
{
bool exit = false;
int[] indices = new int[3] { 0, 0, 0 };
string[][] vectores = new string[3][];
vectores[0] = new string[] { "A", "B", "C" };
vectores[1] = new string[] { "A", "B" };
vectores[2] = new string[] { "B", "D", "E", "F" };
string[] item;
int[] tamaños = new int[3]{vectores[0].GetUpperBound(0),
vectores[1].GetUpperBound(0),
vectores[2].GetUpperBound(0)};
while (!exit)
{
item = new string[]{ vectores[0][indices[0]],
vectores[1][indices[1]],
vectores[2][indices[2]]};
Console.WriteLine("[{0},{1},{2}]={3}{4}{5}", indices[0], indices[1], indices[2], item[0], item[1], item[2]);
GetVector(tamaños, ref indices, ref exit);
}
Console.ReadKey();
}
public static void GetVector(int[] tamaños, ref int[] indices, ref bool exit)
{
for (int i = tamaños.GetUpperBound(0); i >= 0; i--)
{
if (tamaños[i] > indices[i])
{
indices[i]++;
break;
}
else
{
//ULTIMO ITEM EN EL ARRAY, VALIDAR LAS OTRAS DIMENSIONES SI YA ESTA EN EL ULTIMO ITEM
if (!ValidateIndexes(tamaños, indices))
indices[i] = 0;
else
{
exit = true;
break;
}
}
}
}
public static bool ValidateIndexes(int[] tamaños, int[] indices)
{
for (int i = 0; i < tamaños.Length; i++)
{
if (tamaños[i] != indices[i])
return false;
}
return true;
}
The output looks like
[0,0,0]=AAB
[0,0,1]=AAD
[0,0,2]=AAE
[0,0,3]=AAF
[0,1,0]=ABB
[0,1,1]=ABD
[0,1,2]=ABE
[0,1,3]=ABF
[1,0,0]=BAB
[1,0,1]=BAD
[1,0,2]=BAE
[1,0,3]=BAF
[1,1,0]=BBB
[1,1,1]=BBD
[1,1,2]=BBE
[1,1,3]=BBF
[2,0,0]=CAB
[2,0,1]=CAD
[2,0,2]=CAE
[2,0,3]=CAF
[2,1,0]=CBB
[2,1,1]=CBD
[2,1,2]=CBE
[2,1,3]=CBF