EPPlus Pivot Table - Copy Values to New Sheet - epplus

I've successfully created a pivot table using EPPlus. The pivot table resides in a seperate sheet from the raw data. I would like to copy the pivot table data to a new, third, sheet, but just the values, not the pivot defintions. Does EPPlus support this?

You can just copy the datasource range via Cache Definition:
public void PivotDataCopy()
{
const string FORMATCURRENCY = "#,###;[Red](#,###)";
var file = new FileInfo(#"c:\temp\temp.xlsx");
if (file.Exists)
file.Delete();
var pck = new ExcelPackage(file);
var workbook = pck.Workbook;
var dataworksheet = workbook.Worksheets.Add("datasheet");
//The data
dataworksheet.Cells["A20"].Value = "Col1";
dataworksheet.Cells["A21"].Value = "sdf";
dataworksheet.Cells["A22"].Value = "wer";
dataworksheet.Cells["A23"].Value = "ghgh";
dataworksheet.Cells["A24"].Value = "sdf";
dataworksheet.Cells["A25"].Value = "wer";
dataworksheet.Cells["B20"].Value = "Col2";
dataworksheet.Cells["B21"].Value = "Group A";
dataworksheet.Cells["B22"].Value = "Group B";
dataworksheet.Cells["B23"].Value = "Group A";
dataworksheet.Cells["B24"].Value = "Group C";
dataworksheet.Cells["B25"].Value = "Group A";
dataworksheet.Cells["C20"].Value = "Col3";
dataworksheet.Cells["C21"].Value = 453.5;
dataworksheet.Cells["C22"].Value = 634.5;
dataworksheet.Cells["C23"].Value = 274.5;
dataworksheet.Cells["C24"].Value = 453.5;
dataworksheet.Cells["C25"].Value = 634.5;
dataworksheet.Cells["D20"].Value = "Col4";
dataworksheet.Cells["D21"].Value = 686468;
dataworksheet.Cells["D22"].Value = 996440;
dataworksheet.Cells["D23"].Value = 185780;
dataworksheet.Cells["D24"].Value = 686468;
dataworksheet.Cells["D25"].Value = 996440;
//The pivot table
var pivotworksheet = workbook.Worksheets.Add("pivotsheet");
var pivotTable = pivotworksheet.PivotTables.Add(pivotworksheet.Cells["A1"], dataworksheet.Cells["A20:D29"], "test");
//The label row field
pivotTable.RowFields.Add(pivotTable.Fields["Col1"]);
pivotTable.DataOnRows = false;
//The data fields
var field = pivotTable.DataFields.Add(pivotTable.Fields["Col3"]);
field.Name = "Sum of Col2";
field.Function = DataFieldFunctions.Sum;
field.Format = FORMATCURRENCY;
field = pivotTable.DataFields.Add(pivotTable.Fields["Col4"]);
field.Name = "Sum of Col3";
field.Function = DataFieldFunctions.Sum;
field.Format = FORMATCURRENCY;
//Get the pivot table data source
var newworksheet = workbook.Worksheets.Add("newworksheet");
var pivotdatasourcerange = pivotTable.CacheDefinition.SourceRange;
pivotdatasourcerange.Copy(newworksheet.Cells["A1"]);
pck.Save();
}
EDIT: Doing with a VBA macro which then resaves the sheet as a non-macro XLSX:
public void PivotDataCopy()
{
const string FORMATCURRENCY = "#,###;[Red](#,###)";
var file = new FileInfo(#"c:\temp\temp.xlsm");
if (file.Exists)
file.Delete();
var pck = new ExcelPackage(file);
var workbook = pck.Workbook;
var dataworksheet = workbook.Worksheets.Add("datasheet");
//The data
dataworksheet.Cells["A20"].Value = "Col1";
dataworksheet.Cells["A21"].Value = "sdf";
dataworksheet.Cells["A22"].Value = "wer";
dataworksheet.Cells["A23"].Value = "ghgh";
dataworksheet.Cells["A24"].Value = "sdf";
dataworksheet.Cells["A25"].Value = "wer";
dataworksheet.Cells["B20"].Value = "Col2";
dataworksheet.Cells["B21"].Value = "Group A";
dataworksheet.Cells["B22"].Value = "Group B";
dataworksheet.Cells["B23"].Value = "Group A";
dataworksheet.Cells["B24"].Value = "Group C";
dataworksheet.Cells["B25"].Value = "Group A";
dataworksheet.Cells["C20"].Value = "Col3";
dataworksheet.Cells["C21"].Value = 453.5;
dataworksheet.Cells["C22"].Value = 634.5;
dataworksheet.Cells["C23"].Value = 274.5;
dataworksheet.Cells["C24"].Value = 453.5;
dataworksheet.Cells["C25"].Value = 634.5;
dataworksheet.Cells["D20"].Value = "Col4";
dataworksheet.Cells["D21"].Value = 686468;
dataworksheet.Cells["D22"].Value = 996440;
dataworksheet.Cells["D23"].Value = 185780;
dataworksheet.Cells["D24"].Value = 686468;
dataworksheet.Cells["D25"].Value = 996440;
//The pivot table
var pivotworksheet = workbook.Worksheets.Add("pivotsheet");
var pivotTable = pivotworksheet.PivotTables.Add(pivotworksheet.Cells["A1"], dataworksheet.Cells["A20:D29"], "test");
//The label row field
pivotTable.RowFields.Add(pivotTable.Fields["Col1"]);
pivotTable.DataOnRows = false;
//The data fields
var field = pivotTable.DataFields.Add(pivotTable.Fields["Col3"]);
field.Name = "Sum of Col2";
field.Function = DataFieldFunctions.Sum;
field.Format = FORMATCURRENCY;
field = pivotTable.DataFields.Add(pivotTable.Fields["Col4"]);
field.Name = "Sum of Col3";
field.Function = DataFieldFunctions.Sum;
field.Format = FORMATCURRENCY;
//add the macro to copy the table data on open
workbook.Worksheets.Add("newworksheet");
var sb = new StringBuilder();
sb.AppendLine("Private Sub Workbook_SheetCalculate(ByVal Sh As Object)");
sb.AppendLine(" Sheets(\"pivotsheet\").Cells.Copy");
sb.AppendLine(" Sheets(\"newworksheet\").Range(\"A1\").PasteSpecial Paste:=xlPasteValues");
sb.AppendLine(" Selection.Clear");
sb.AppendLine(" Application.DisplayAlerts = False");
sb.AppendLine(String.Format(" ActiveWorkbook.SaveAs \"{0}\", xlOpenXMLWorkbook", file.FullName.Replace("xlsm", "xlsx")));
sb.AppendLine(" Application.DisplayAlerts = True");
sb.AppendLine("End Sub");
pck.Workbook.CreateVBAProject();
pck.Workbook.CodeModule.Code = sb.ToString();
pck.Save();
}

Related

How to get the value from a table cell

I have a table I created and am most interested in the "costs" column. I could have more than one row of data in which I would have multiple costs and I want to add those up and put the result in the table cell next to "Total." I saw a method called GetValue(); but not sure if that is what I am looking for or how to use it. My thinking is that migradoc has a method where you can get the value of a table cell in which I would store that in a variable. And where I create the "Total" row, I would use that variable to display the total. So how would I do that?
My code:
/define header of table
Row row = table.AddRow();
row.HeadingFormat = true;
Cell cell = row.Cells[0];
cell.AddParagraph("Customer Name");
cell.Format.Font.Bold = true;
cell = row.Cells[1];
cell.AddParagraph("Date Created");
cell.Format.Font.Bold = true;
cell = row.Cells[2];
cell.AddParagraph("Description");
cell.Format.Font.Bold = true;
cell = row.Cells[3];
cell.AddParagraph("Due Date");
cell.Format.Font.Bold = true;
cell = row.Cells[4];
cell.AddParagraph("Billable Hours");
cell.Format.Font.Bold = true;
cell = row.Cells[5];
cell.AddParagraph("Costs");
cell.Format.Font.Bold = true;
//define a row of data in the table
foreach (TicketView1 ticket in SampleTickets)
{
row = table.AddRow();
cell = row.Cells[0];
cell.AddParagraph(ticket.customer_name);
cell = row.Cells[1];
cell.AddParagraph(ticket.date_created.ToString("MM/dd/yyyy"));
cell = row.Cells[2];
cell.AddParagraph(ticket.description);
cell = row.Cells[3];
cell.AddParagraph(ticket.due_date.ToString("MM/dd/yyyy"));
cell = row.Cells[4];
cell.AddParagraph(ticket.billable_hrs.ToString());
}
cell = row.Cells[5];
cell.AddParagraph("$60.00");
//add invisible row as a space line to the table
row = table.AddRow();
row.Borders.Visible = false;
//add the subtotal row
row = table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("Sub Total:");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
cell = row.Cells[5];
//add tax row
row = table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("TAX:");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
//add total
row = table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("TOTAL:");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
}
I have found out the answer by simple creating a count variable and then inside my forEach loop just adding that sum plus my dummy data which would be 60. Then right where I created my "totals" row, right underneath it I added into the cell that variable which contains the count data.
My forEach loop and count variable
double sum = 0;
foreach (TicketView1 ticket in SampleTickets)
{
row = table.AddRow();
cell = row.Cells[0];
cell.AddParagraph(ticket.customer_name);
cell = row.Cells[1];
cell.AddParagraph(ticket.date_created.ToString("MM/dd/yyyy"));
cell = row.Cells[2];
cell.AddParagraph(ticket.description);
cell = row.Cells[3];
cell.AddParagraph(ticket.due_date.ToString("MM/dd/yyyy"));
cell = row.Cells[4];
cell.AddParagraph(ticket.billable_hrs.ToString());
cell = row.Cells[5];
cell.AddParagraph("$60.00");
sum = sum + 60;
}
Where I added the total row and appended the data to the totals cell:
//add total
row = table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("TOTAL:");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
row.Cells[5].AddParagraph(sum.ToString("$0.00"));

c# EPPlus: how to fix - ShowFilter doesn't work

The issue with Table.ShowFilter, using EPPLUS library.
Created new ExcelTable in the sheet, but can't apply Table.ShowFilter = false.
The filters are still in the table.
TableStyle and StyleName work fine.
ExcelTable et = (ExcelTable)Table;
int firstRow = newRow;
int lastRow;
if (DataStructure.Data != null)
lastRow = newRow + DataStructure.Data.Count();
else
lastRow = newRow + 1;
int firstColumn = OriginalAddress.StartColumn;
int lastColumn = OriginalAddress.EndColumn;
ExcelRange rg = ws.Cells[firstRow, firstColumn, lastRow, lastColumn];
Guid guid = Guid.NewGuid();
string str_guid = guid.ToString();
string tableName = et.Name + "_" + str_guid;
ExcelTable tab = wsTarget.Tables.Add(rg, tableName);
// tab.ShowHeader = et.ShowHeader;
// tab.TableStyle = et.TableStyle;
// tab.StyleName = et.StyleName;
tab.ShowFilter = false;
Right table is desired result.
Please, help to fix!
Is this you want to mean?
tab.ShowHeader = false;
This very little example works here :
using (ExcelPackage xls = new ExcelPackage())
{
ExcelWorksheet ws2 = xls.Workbook.Worksheets.Add("f1");
OfficeOpenXml.Table.ExcelTable tab = ws2.Tables.Add(new ExcelAddressBase("a1:e5"), "table");
//tab.TableStyle = et.TableStyle;
//tab.StyleName = et.StyleName;
tab.ShowFilter = false;
FileInfo f = new FileInfo(#"d:\temp\test.xlsx");
xls.SaveAs(f);
}
Probably, the bug.
Adding ws2.DeleteColumn(1, 1) to the code, add filters to the table, even with tab.ShowFilter = false.
Workaround: put tab.ShowFilter = false right before file saving.
using (ExcelPackage xls = new ExcelPackage())
{
ExcelWorksheet ws2 = xls.Workbook.Worksheets.Add("f1");
OfficeOpenXml.Table.ExcelTable tab = ws2.Tables.Add(new ExcelAddressBase("d3:g8"), "table");
//tab.TableStyle = et.TableStyle;
//tab.StyleName = et.StyleName;
ws2.DeleteColumn(1, 1);
tab.ShowFilter = false;
FileInfo f = new FileInfo(#"test.xlsx");
xls.SaveAs(f);
}

Automation Error VBA from Userform

Keep getting an automation error when my userform uses the below code when it initializes. I dont get an error when i take it out. The userform is being called from a shape using a module. the text from the userform is stored under a sheet called "Compliance". My userform is also called compliance. Here is my code below, any help would be much appreciated:
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Set ws = Sheets("compliance")
On Error Resume Next
With page1
employee1.Value = ws.Range("B2")
employee2.Value = ws.Range("B3")
employee3.Value = ws.Range("B4")
employee4.Value = ws.Range("B5")
employee5.Value = ws.Range("B5")
employee6.Value = ws.Range("B6")
'//// setting ops grade
grade1.Value = ws.Range("D2")
grade2.Value = ws.Range("D3")
grade3.Value = ws.Range("D4")
grade4.Value = ws.Range("D5")
grade5.Value = ws.Range("D6")
grade6.Value = ws.Range("D7")
'//// setting employee compliance recap
recap1.Value = ws.Range("F2")
recap2.Value = ws.Range("F3")
recap3.Value = ws.Range("F4")
recap4.Value = ws.Range("F5")
recap5.Value = ws.Range("F6")
recap6.Value = ws.Range("F7")
'////////////////////////////////////////// febraury
'//// setting employees interviewed
With page2
febemp1.Value = ws.Range("B14")
febemp2.Value = ws.Range("B15")
febemp3.Value = ws.Range("B16")
febemp4.Value = ws.Range("B17")
febemp5.Value = ws.Range("B18")
febemp6.Value = ws.Range("B19")
febgrd1.Value = ws.Range("D14")
febgrd2.Value = ws.Range("D15")
febgrd3.Value = ws.Range("D16")
febgrd4.Value = ws.Range("D17")
febgrd5.Value = ws.Range("D18")
febgrd6.Value = ws.Range("D19")
febrecap1.Value = ws.Range("F14")
febrecap2.Value = ws.Range("F15")
febrecap3.Value = ws.Range("F16")
febrecap4.Value = ws.Range("F17")
febrecap5.Value = ws.Range("F18")
febrecap6.Value = ws.Range("F19")
With page3
marchemp1.Value = ws.Range("B26")
marchemp2.Value = ws.Range("B27")
marchemp3.Value = ws.Range("B28")
marchemp4.Value = ws.Range("B29")
marchemp5.Value = ws.Range("B30")
marchemp6.Value = ws.Range("B31")
marchgrd1.Value = ws.Range("D26")
marchgrd2.Value = ws.Range("D27")
marchgrd3.Value = ws.Range("D28")
marchgrd4.Value = ws.Range("D29")
marchgrd5.Value = ws.Range("D30")
marchgrd6.Value = ws.Range("D31")
marchrecap1.Value = ws.Range("F26")
marchrecap2.Value = ws.Range("F27")
marchrecap3.Value = ws.Range("F28")
marchrecap4.Value = ws.Range("F29")
marchrecap5.Value = ws.Range("F30")
marchrecap6.Value = ws.Range("F31")
With page4
apremp1.Value = ws.Range("B38")
apremp2.Value = ws.Range("B39")
apremp3.Value = ws.Range("B40")
apremp4.Value = ws.Range("B41")
apremp5.Value = ws.Range("B42")
apremp6.Value = ws.Range("B43")
aprgrd1.Value = ws.Range("D38")
aprgrd2.Value = ws.Range("D39")
aprgrd3.Value = ws.Range("D40")
aprgrd4.Value = ws.Range("D41")
aprgrd5.Value = ws.Range("D42")
aprgrd6.Value = ws.Range("D43")
aprrecap1.Value = ws.Range("F38")
aprrecap2.Value = ws.Range("F39")
aprrecap3.Value = ws.Range("F40")
aprrecap4.Value = ws.Range("F41")
aprrecap5.Value = ws.Range("F42")
aprrecap6.Value = ws.Range("F43")
With page5
mayemp1.Value = ws.Range("B50")
mayemp2.Value = ws.Range("B51")
mayemp3.Value = ws.Range("B52")
mayemp4.Value = ws.Range("B53")
mayemp5.Value = ws.Range("B54")
mayemp6.Value = ws.Range("B55")
maygrd1.Value = ws.Range("D50")
maygrd2.Value = ws.Range("D51")
maygrd3.Value = ws.Range("D52")
maygrd4.Value = ws.Range("D53")
maygrd5.Value = ws.Range("D54")
maygrd6.Value = ws.Range("D55")
mayrecap1.Value = ws.Range("F50")
mayrecap2.Value = ws.Range("F51")
mayrecap3.Value = ws.Range("F52")
mayrecap4.Value = ws.Range("F53")
mayrecap5.Value = ws.Range("F54")
mayrecap6.Value = ws.Range("F55")
With page6
junemp1.Value = ws.Range("B62")
junemp2.Value = ws.Range("B63")
junemp3.Value = ws.Range("B64")
junemp4.Value = ws.Range("B65")
junemp5.Value = ws.Range("B66")
junemp6.Value = ws.Range("B67")
jungrd1.Value = ws.Range("D62")
jungrd2.Value = ws.Range("D63")
jungrd3.Value = ws.Range("D64")
jungrd4.Value = ws.Range("D65")
jungrd5.Value = ws.Range("D66")
jungrd6.Value = ws.Range("D67")
junrecap1.Value = ws.Range("F62")
junrecap2.Value = ws.Range("F63")
junrecap3.Value = ws.Range("F64")
junrecap4.Value = ws.Range("F65")
junrecap5.Value = ws.Range("F66")
junrecap6.Value = ws.Range("F67")
With page7
julemp1.Value = ws.Range("B74")
julemp2.Value = ws.Range("B75")
julemp3.Value = ws.Range("B76")
julemp4.Value = ws.Range("B77")
julemp5.Value = ws.Range("B78")
julemp6.Value = ws.Range("B79")
julgrd1.Value = ws.Range("D74")
julgrd2.Value = ws.Range("D75")
julgrd3.Value = ws.Range("D76")
julgrd4.Value = ws.Range("D77")
julgrd5.Value = ws.Range("D78")
julgrd6.Value = ws.Range("D79")
julrecap1.Value = ws.Range("F74")
julrecap2.Value = ws.Range("F75")
julrecap3.Value = ws.Range("F76")
julrecap4.Value = ws.Range("F77")
julrecap5.Value = ws.Range("F78")
julrecap6.Value = ws.Range("F79")
With page8
augemp1.Value = ws.Range("B86")
augemp2.Value = ws.Range("B87")
augemp3.Value = ws.Range("B88")
augemp4.Value = ws.Range("B89")
augemp5.Value = ws.Range("B90")
augemp6.Value = ws.Range("B91")
auggrd1.Value = ws.Range("D86")
auggrd2.Value = ws.Range("D87")
auggrd3.Value = ws.Range("D88")
auggrd4.Value = ws.Range("D89")
auggrd5.Value = ws.Range("D90")
auggrd6.Value = ws.Range("D91")
augrecap1.Value = ws.Range("F86")
augrecap2.Value = ws.Range("F87")
augrecap3.Value = ws.Range("F88")
augrecap4.Value = ws.Range("F89")
augrecap5.Value = ws.Range("F90")
augrecap6.Value = ws.Range("F91")
With page9
sepemp1.Value = ws.Range("B98")
sepemp2.Value = ws.Range("B99")
sepemp3.Value = ws.Range("B100")
sepemp4.Value = ws.Range("B101")
sepemp5.Value = ws.Range("B102")
sepemp6.Value = ws.Range("B103")
sepgrd1.Value = ws.Range("D98")
sepgrd2.Value = ws.Range("D99")
sepgrd3.Value = ws.Range("D100")
sepgrd4.Value = ws.Range("D101")
sepgrd5.Value = ws.Range("D102")
sepgrd6.Value = ws.Range("D103")
seprecap1.Value = ws.Range("F98")
seprecap2.Value = ws.Range("F99")
seprecap3.Value = ws.Range("F100")
seprecap4.Value = ws.Range("F101")
seprecap5.Value = ws.Range("F102")
seprecap6.Value = ws.Range("F103")
With page10
octemp1.Value = ws.Range("B110")
octemp2.Value = ws.Range("B111")
octemp3.Value = ws.Range("B112")
octemp4.Value = ws.Range("B113")
octemp5.Value = ws.Range("B114")
octemp6.Value = ws.Range("B115")
octgrd1.Value = ws.Range("D110")
octgrd2.Value = ws.Range("D111")
octgrd3.Value = ws.Range("D112")
octgrd4.Value = ws.Range("D113")
octgrd5.Value = ws.Range("D114")
octgrd6.Value = ws.Range("D115")
octrecap1.Value = ws.Range("F110")
octrecap2.Value = ws.Range("F111")
octrecap3.Value = ws.Range("F112")
octrecap4.Value = ws.Range("F113")
octrecap5.Value = ws.Range("F114")
octrecap6.Value = ws.Range("F115")
With page11
novemp1.Value = ws.Range("B122")
novemp2.Value = ws.Range("B123")
novemp3.Value = ws.Range("B124")
novemp4.Value = ws.Range("B125")
novemp5.Value = ws.Range("B126")
novemp6.Value = ws.Range("B127")
novgrd1.Value = ws.Range("D122")
novgrd2.Value = ws.Range("D123")
novgrd3.Value = ws.Range("D124")
novgrd4.Value = ws.Range("D125")
novgrd5.Value = ws.Range("D126")
novgrd6.Value = ws.Range("D127")
novrecap1.Value = ws.Range("F122")
novrecap2.Value = ws.Range("F123")
novrecap3.Value = ws.Range("F124")
novrecap4.Value = ws.Range("F125")
novrecap5.Value = ws.Range("F126")
novrecap6.Value = ws.Range("F127")
With page12
decemp1.Value = ws.Range("B134")
decemp2.Value = ws.Range("B135")
decemp3.Value = ws.Range("B136")
decemp4.Value = ws.Range("B137")
decemp5.Value = ws.Range("B138")
decemp6.Value = ws.Range("B139")
decgrd1.Value = ws.Range("D134")
decgrd2.Value = ws.Range("D135")
decgrd3.Value = ws.Range("D136")
decgrd4.Value = ws.Range("D137")
decgrd5.Value = ws.Range("D138")
decgrd6.Value = ws.Range("D139")
decrecap1.Value = ws.Range("F134")
decrecap2.Value = ws.Range("F135")
decrecap3.Value = ws.Range("F136")
decrecap4.Value = ws.Range("F137")
decrecap5.Value = ws.Range("F138")
decrecap6.Value = ws.Range("F139")
End With
End With
End With
End With
End With
End With
End With
End With
End With
End With
End With
End With
End Sub
Untested - too many controls to make...
You'd need to harmonize your control naming a bit, but something like this should work:
Private Sub UserForm_Initialize()
Dim ws As Worksheet, mNum As Long, months, i As Long, m
Dim c As Range
Set ws = Sheets("compliance")
Set c = ws.Range("B1")
months = Array("jan", "feb", "mar", "apr", "may", "jun", _
"jul", "aug", "sep", "oct", "nov", "dec")
For mNum = 1 To 12
m = months(m - 1)
With Me.MultiPage1.Pages("page" & mNum)
For i = 1 To 6
.Controls(m & "emp" & i).Value = c.Offset(i, 0).Value
.Controls(m & "grd" & i).Value = c.Offset(i, 2).Value
.Controls(m & "recap" & i).Value = c.Offset(i, 4).Value
Next i
End With
Set c = c.Offset(12, 0)
Next mNum
End Sub

How to insert new value autonumber by parameters to access

When I press insert, I receive the error: Syntax error in INSERT INTO statement
EmployeeID (Autonumber)
EmployeeName(Text)
Position(Text)
Address(Text)
OleDbDataAdapter ad;
DataSet ds;
DataTable dt;
protected void SetInsertParameters()
{
string sql = "INSERT INTO Employee(EmployeeName,Position,Address)"+
" VALUES (#EmployeeName,#Position,#Address)";
ad.InsertCommand = new OleDbCommand(sql, con);
OleDbParameter param = new OleDbParameter("#EmployeeName", OleDbType.VarChar);
param.SourceColumn = "EmployeeName";
param.SourceVersion = DataRowVersion.Current;
ad.InsertCommand.Parameters.Add(param);
param = new OleDbParameter("#Position", OleDbType.VarChar);
param.SourceColumn = "Position";
param.SourceVersion = DataRowVersion.Current;
ad.InsertCommand.Parameters.Add(param);
param = new OleDbParameter("#Address", OleDbType.VarChar);
param.SourceColumn = "Address";
param.SourceVersion = DataRowVersion.Current;
ad.InsertCommand.Parameters.Add(param);
}
void InsertNewValues()
{
dt = ds.Tables["Employee"];
DataRow row = dt.NewRow();
row[0] = txt_employeeID.Text;
row[1] = txt_name.Text;
row[2] = txt_position.Text;
row[3] = txt_address.Text;
dt.Rows.Add(row);
ad.Update(ds, "Employee");
ad.Fill(ds);
}
Position is a reserved word and must be bracketed:
"INSERT INTO Employee(EmployeeName,[Position],Address)"

Pivot table in VB.net

i have created the pivot table using vb.net code
pivotField1 = pivotTable.PivotFields(14)
pivotField2 = pivotTable.PivotFields(15)
pivotField3 = pivotTable.PivotFields(16)
pivotField4 = pivotTable.PivotFields(17)
pivotField5 = pivotTable.PivotFields(18)
pivotField1.Orientation = XlPivotFieldOrientation.xlDataField
pivotField2.Orientation = XlPivotFieldOrientation.xlDataField
pivotField3.Orientation = XlPivotFieldOrientation.xlDataField
pivotField4.Orientation = XlPivotFieldOrientation.xlDataField
pivotField5.Orientation = XlPivotFieldOrientation.xlDataField
pivotField1.Function = XlConsolidationFunction.xlSum
pivotField2.Function = XlConsolidationFunction.xlSum
pivotField3.Function = XlConsolidationFunction.xlSum
pivotField4.Function = XlConsolidationFunction.xlSum
pivotField5.Function = XlConsolidationFunction.xlSum
But it displays in pivot tables as Sum-Value in Rowlable but i want to ColumnLabel so please help....
Use this... May help :)
Dim ptSigmaRow As PivotField
For Each ptSigmaRow In ptTable.RowFields
Dim d As String = ptSigmaRow.Name
If ptSigmaRow.Name = "Data" Then
ptSigmaRow.Orientation = XlPivotFieldOrientation.xlColumnField
End If
Next