簡體   English   中英

將文本添加到Excel導出列表視圖的第1行

[英]Add text to row 1 in excel export listview

我想為excel工作表第一行中的每一列命名,在命名列的第一行出現異常。 請指教?

Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
            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 = 2;
        int j = 1;

        if (comboBox1.Text == "Brickcom")
        {
            try
            {
                ws.Rows[j, 1] = "Category";
                ws.Rows[j, 2] = "Part Number";
                ws.Rows[j, 3] = "TradePrice";
                ws.Rows[j, 4] = "Product";
                ws.Rows[j, 5] = "Resolution";
                ws.Rows[j, 6] = "Included Accessories";
                ws.Rows[j, 7] = "Major Acc Price";            


                foreach (ListViewItem comp in listView1.Items)
                {

                    ws.Cells[i, j] = comp.Text.ToString();

                    foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)
                    {
                        ws.Cells[i, j] = drv.Text.ToString();

                        j++;
                    }

                    j = 1;
                    i++;                        
                }
                xla.Visible = true;
            }
            catch
            {
                MessageBox.Show("Export did not work");
            }
        }

嘗試這個:

            ws.Cells[j, 1] = "Category";
            ws.Cells[j, 2] = "Part Number";
            ws.Cells[j, 3] = "TradePrice";
            ...

我會使用流作家:

    SaveFileDialog savefile = new SaveFileDialog();
    savefile.Filter = "CSV|*.csv";
    savefile.RestoreDirectory = true;
    savefile.ShowDialog();
    string filename = savefile.FileName;
    if (filename != "")
   {
    StreamWriter ofile = new StreamWriter(filename);
    ofile.WriteLine("Category, Part Number, TradePrice, ..."); //just separate using a comma ,

    //your complete writings here

    ofile.Close(); //close the streamwriter and you're done
   }

祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM