将数据导出到预定义的Excel模板。 [英] Export data to predefined excel template.

查看:95
本文介绍了将数据导出到预定义的Excel模板。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序。我可以使用Excel Interop将数据导出到Excel。后来我手动进行计算。在这里,我想使用已经在Sheet1&中编写公式的Excel。我从我的应用程序导出的数据应该加载到Sheet2中。数据完全加载后,结果应显示在Sheet1中。 Pl,帮助这个请求。

以下是我可以将数据导出到新excel的代码。



我尝试了什么:



I created one application. I can export data to Excel using Excel Interop. Later I am doing calculation manually. Here I want use the Excel which already I written formulas in Sheet1 & the data which I exporting from my application should load in Sheet2. Once data loaded completely, result should display in Sheet1. Pl, help on this request.
Below is the Code that I can export data to new excel.

What I have tried:

public void ExportToExcel(ListView lsvData, string sSheeyName)
        {
            Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
            xla.Visible = true;
            Microsoft.Office.Interop.Excel.Workbook wb = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)xla.ActiveSheet;
            int i = 4;
            int j = 1;
            int c = 1;
            int r = 5;
            int ilsvCol = 0;
            string sFcol;
            ws.Name = sSheeyName;

            ws.Cells[c, r] = ws.Name;
            ws.Cells[c, r].Font.Bold = true;
            ws.Cells[c, r].Font.Size = 20;

            foreach (ColumnHeader lsvcolumn in lsvData.Columns)
            {              
                if (lsvcolumn.Width > 0)
                {                   
                    ws.Cells[i, j] = lsvcolumn.Text;
                    ws.Cells[i, j].Font.Bold = true;
                    ws.Cells[i, j].Interior.Color = Color.GreenYellow;
                    ws.Columns[j].ColumnWidth = lsvcolumn.Text.Length + 10;

                    j++;
                }
            }
            if (j > 52)
            {
                sFcol = "B" + Convert.ToChar(64 + j - 52).ToString();
            }
            else if (j > 26)
            {
                sFcol = "A" + Convert.ToChar(64 + j - 26).ToString();
            }
            else
            {
                sFcol = Convert.ToChar(64 + j).ToString();
            }

            i = 5;
            j = 1;
            foreach (ListViewItem comp in lsvData.Items)
            {
                ilsvCol = 0;
                foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)
                {
                   // if (lsvData.Columns[ilsvCol].Width > 0)
                    if (lsvData.Columns[ilsvCol].Width > 0)

                        {
                        // ws.Cells[i, j] = "'" + drv.Text.ToString();
                        ws.Cells[i, j] =  drv.Text.ToString();
                        ws.Cells[i, j].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                        j++;
                    }
                    ilsvCol++;
                }
                j = 1;
                i++;
            }

        }

    }

推荐答案

你可以尝试这个图片 closedxml

i用于某个项目并且工作正常。
you can try this libray closedxml.
i used for some project and worked fine.


这篇关于将数据导出到预定义的Excel模板。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆