Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to add empty rows in my data table and then loop through the column heading and split by- Then year insert to the empty row and After that merge the year row

What I have tried:

Here is my data table code:

private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
Excel.Range chartRange;

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

xlWorkSheet.Cells[2, 1] = "Retailer";

xlWorkSheet.Cells[2, 2] = "Brand";
xlWorkSheet.Cells[2, 3] = "2019_WK1";

xlWorkSheet.Cells[2, 4] = "2019_WK2";

xlWorkSheet.Cells[2, 5] = "2019_WK3";


xlWorkSheet.Cells[3, 1] = "LuLu";
xlWorkSheet.Cells[3, 2] = "Perisil";
xlWorkSheet.Cells[3, 3] = "25";
xlWorkSheet.Cells[3, 4] = "26";
xlWorkSheet.Cells[3, 5] = "23";

xlWorkSheet.Cells[4, 1] = "Lulu";
xlWorkSheet.Cells[4, 2] = "Ariel";
xlWorkSheet.Cells[4, 3] = "26";
xlWorkSheet.Cells[4, 4] = "28";
xlWorkSheet.Cells[4, 5] = "29";

xlWorkSheet.Cells[5, 1] = "Danube";
xlWorkSheet.Cells[5, 2] = "Omo";
xlWorkSheet.Cells[5, 3] = "27";
xlWorkSheet.Cells[5, 4] = "28";
xlWorkSheet.Cells[5, 5] = "30";


xlWorkSheet.Cells[6, 1] = "Danube";
xlWorkSheet.Cells[6, 2] = "Tide";
xlWorkSheet.Cells[6, 3] = "24";
xlWorkSheet.Cells[6, 4] = "23";
xlWorkSheet.Cells[6, 5] = "29";


xlWorkSheet.Cells[7, 1] = "Bin Dawood";
xlWorkSheet.Cells[7, 2] = "Persil";
xlWorkSheet.Cells[7, 3] = "26";
xlWorkSheet.Cells[7, 4] = "27";
xlWorkSheet.Cells[7, 5] = "28";


xlWorkBook.SaveAs("F:\\CTR_Data", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);

MessageBox.Show("File created !");
}

I want to add five empty rows in ("A1" to "E1" cells) and loop through the column heading and split by-
Posted
Updated 30-May-19 3:24am
Comments
CHill60 30-May-19 7:39am    
a1 to e1 would be five empty columns not rows. The easiest way would be to just put your data in the correct columns in the first place
Richard MacCutchan 30-May-19 7:40am    
Start by removing all those fixed row and column numbers and replace them by variables so you can add other rows at the required positions.
Member 14376506 30-May-19 7:42am    
How to replace it veriables...can you give an example please

Try this before call xlWorkBook.SaveAs:
C#
Range line = (Range)xlWorkSheet.Rows[1];
for (int i = 0; i < 5; i++) line.Insert(XlInsertShiftDirection.xlShiftDown, false);

However, I suggest to experiment a little with EPPlus. Just for fun, who knows, maybe you'll love it :)
 
Share this answer
 
Quote:
How to add empty rows in my data table and then loop through the column heading and split by- Then year insert to the empty row and After that merge the year row

Basically, your app is remote controlling Excel, so anything you do on Excel can be done on remote. It is interesting because Excel can show you how your actions translate to code.
Excel have a feature: recording a macro.
When you record a macro, all your actions are translated to VBA code. After end of recording, just have to go to VBE (Alt+F11) to see the code generated.
Even if VBA, the generated code will be close enough to what you need to be useful.
https://trumpexcel.com/record-macro-vba/[^]
Record a Macro in Excel - Instructions and Video Lesson[^]
How to Record a Macro in Microsoft Excel | Webucator[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900