【问题标题】:How to add empty rows in excel sheet data table using c#如何使用c#在excel工作表数据表中添加空行
【发布时间】:2019-05-30 09:39:05
【问题描述】:

如何在我的数据表中添加空行,然后循环遍历列标题并拆分-然后将年份插入空行并 之后合并年行

这是我的数据表代码:

       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 !");
        }    

我想在(“A1”到“E1”单元格)中添加五个空行,然后循环遍历列标题并按-

我想要一个类似上图的excel输出

【问题讨论】:

  • 您能否向我们展示解决方案完成后工作表应该是什么样子的示例?您的问题在您想要做什么方面非常令人困惑。我看不到要拆分的-,并且您说要添加多行(复数),然后声明您想将年份插入空行(单数)。工作表的前后图像可能更有帮助。
  • 我更新了一张图片我想要什么类型的输出
  • 我认为这是一些课堂作业,您更改了给定的电子表格?对吗?

标签: c# excel


【解决方案1】:

必须使用合并方法(xlWorkSheet.Range[xlWorkSheet.Cells[1, 1], xlWorkSheet.Cells[2, 1]].Merge();)

Application xlApp;
        Workbook xlWorkBook;
        Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        Range chartRange;

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

        xlWorkSheet.Cells[1, 1] = "Retailer";
        xlWorkSheet.Range[xlWorkSheet.Cells[1, 1], xlWorkSheet.Cells[2, 1]].Merge();

        xlWorkSheet.Cells[2, 2] = "Brand";
        xlWorkSheet.Range[xlWorkSheet.Cells[1, 2], xlWorkSheet.Cells[2, 2]].Merge();

        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";


        xlWorkSheet.Cells[1, 3] = "Year";
        xlWorkSheet.Range[xlWorkSheet.Cells[1, 3], xlWorkSheet.Cells[1, 5]].Merge();

        xlWorkBook.SaveAs("test", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

【讨论】:

  • 非常感谢亲爱的...但是列(零售商)和(品牌)我只想要一个单元格而不是合并单元格,类似的列(WK1,WK2,WK3)以及(年)在循环下......你知道怎么做吗
  • 但列(零售商)&(品牌)我只希望在一个单元格中而不是在合并单元格中这绝对不是您的图像所显示的。就循环而言,您只需要一个从 3 到 5 的迭代器并使用该迭代器获取 .cells[2,i] 的文本将其拆分到 '_' 并将 .cells[1,3] 设置为 0 元素和 .cells[2,i]到第 1 个元素。
猜你喜欢
  • 1970-01-01
  • 2014-10-28
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 2022-06-24
相关资源
最近更新 更多