Hi,
How to remove all unwanted spaces from excel column by using invoke code activity. I am using below code for pivot table and in ws worksheet , Perticlular or all columns i want to trim or remove spaces in all rows. How to update. Let me know if you have different approach
Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim ws As Microsoft.Office.Interop.Excel.Worksheet
Dim p_ws As Microsoft.Office.Interop.Excel.Worksheet
Dim rng As Microsoft.Office.Interop.Excel.Range âcapturing the range of sheet1 âââ
Dim p_rng As Microsoft.Office.Interop.Excel.Range âcapturing the range of pivottable shhet1ââ
âDim e_rng As Microsoft.Office.Interop.Excel.Range
Dim x As Integer
Try
excel = New Microsoft.Office.Interop.Excel.ApplicationClassâcreate the instance of excel work bookâ
wb = excel.Workbooks.Open(sPath)âOpen the excel the fileâ
excel.Visible=True
ws=CType(wb.Sheets(âDXC Technologyâ),Microsoft.Office.Interop.Excel.Worksheet)âselect a sheet and activiatesâ
ws.Activate
ws.Columns.AutoFit()
x=ws.UsedRange.Rows.Count
rng=ws.Range(âA1â,âGâ+x.ToString)âSelects the given range of sheet 1â
p_ws=CType(wb.Sheets.Add,Microsoft.Office.Interop.Excel.Worksheet)âcreate a empty and and name the sheet as pivot tableâ
p_ws.Name=âReview Summary Reportâ
p_rng= CType(p_ws.Cells(8,1),Microsoft.Office.Interop.Excel.Range) âSpecify the first cell for the pivot tableâ
'Creation pivot Cache and pivot table â
Dim oPivotCache As Microsoft.Office.Interop.Excel.PivotCache=Nothing
Dim oPivotTables As Microsoft.Office.Interop.Excel.PivotTables=Nothing
Dim oPivotTable As Microsoft.Office.Interop.Excel.PivotTable=Nothing
oPivotCache=CType(wb.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase,rng),Microsoft.Office.Interop.Excel.PivotCache)
oPivotTables=DirectCast(p_ws.PivotTables(Type.Missing),Microsoft.Office.Interop.Excel.PivotTables)
oPivotTable=oPivotTables.Add(oPivotCache,p_rng,âReview_Summary_Reportâ,Type.Missing,Type.Missing)âfirst paramenter is cache,range the table should start,table nameâ
oPivotTable.PivotCache.Refresh
oPivotTable.EnableFieldList=False
âCreation of pivot Fieldsâ
Dim oPivotField As Microsoft.Office.Interop.Excel.PivotField=Nothing
oPivotField=CType(oPivotTable.PivotFields(âReviewRatingâ),Microsoft.Office.Interop.Excel.PivotField)âSpecfiy the row nameâ
oPivotField.Orientation=Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlRowField
oPivotField=CType(oPivotTable.PivotFields(âReviewedPortalâ),Microsoft.Office.Interop.Excel.PivotField)âSpecfiy the coulmn nameâ
oPivotField.Orientation=Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField
oPivotField=CType(oPivotTable.PivotFields(âPostedDateâ),Microsoft.Office.Interop.Excel.PivotField)âSpecfiy the coulmn nameâ
oPivotField.Orientation=Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField
oPivotField=CType(oPivotTable.PivotFields(âClient Commentâ),Microsoft.Office.Interop.Excel.PivotField)âSpecfiy the coulmn nameâ
oPivotField.Orientation=Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField
oPivotField=CType(oPivotTable.PivotFields(âReviewRatingâ),Microsoft.Office.Interop.Excel.PivotField)âSpecfiy the coulmn nameâ
oPivotField.Orientation=Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlDataField
oPivotField.Function=Microsoft.Office.Interop.Excel.XlConsolidationFunction.xlCount
oPivotField.Name=âReview Rating Vs Review Portalâ
p_ws.Cells(1, 3)= "'PROJECT NAME : " & UCase(strProjectName)
p_ws.Range(âC1:E1â).Merge
p_ws.Cells(2, 3)= â'REVIEW RATING Vs REVIEW PORTALâ
p_ws.Range(âC2:E2â).Merge
p_ws.Columns.AutoFit()
excel.DisplayAlerts = False
excel.ActiveWorkbook.Save
excel.ActiveWorkbook.Close
Catch es As Exception
System.Windows.MessageBox.Show(es.Message)
End Try