列表框到 Excel

发布于 2024-12-03 20:41:08 字数 826 浏览 2 评论 0原文

好的,所以我有一个类,客户,我用它来处理数据,然后添加到列表框,如下所示:

   //Submit data from current form         
   customer aCustomer = new customer(comboBox1.Text, textBox1.Text, ChannelSelecBox.Text,
     PurchaserTextBox.Text, NameTextBox.Text, emailTextBox.Text, AddressTextBox.Text, StateBox.Text,
     PayMethodDropDown.Text, Prod1Num.Value.ToString(), Prod2Num.Value.ToString(), 
    Prod3Num.Value.ToString(), Prod4Num.Value.ToString(), Prod5Num.Value.ToString(), 
    Prod6Num.Value.ToString(), SubTData.Text, DiscountTextBox.Text, TaxData.Text, CommentTextBox.Text, 
    ShipData.Text, TotalData.Text);

           // Add aCustomer to ListBox
           Orders.Items.Add(aCustomer);

我有一个字符串覆盖,以便列表框只显示购买者和日期。 现在,我想获取输入列表框中的所有订单,并将大部分数据放入 Excel 电子表格中,每个订单放入其自己的列中。我怎么能这样做呢?

如果您需要更多信息或查看更多我的代码,请告诉我。

Ok, so I have a class, customer, that I use to process data, and then add to a list box, like below:

   //Submit data from current form         
   customer aCustomer = new customer(comboBox1.Text, textBox1.Text, ChannelSelecBox.Text,
     PurchaserTextBox.Text, NameTextBox.Text, emailTextBox.Text, AddressTextBox.Text, StateBox.Text,
     PayMethodDropDown.Text, Prod1Num.Value.ToString(), Prod2Num.Value.ToString(), 
    Prod3Num.Value.ToString(), Prod4Num.Value.ToString(), Prod5Num.Value.ToString(), 
    Prod6Num.Value.ToString(), SubTData.Text, DiscountTextBox.Text, TaxData.Text, CommentTextBox.Text, 
    ShipData.Text, TotalData.Text);

           // Add aCustomer to ListBox
           Orders.Items.Add(aCustomer);

I have a string override so that the listbox just displays the purchaser and the date.
Now I want to take all the orders entered into the list box and put, most of, this data into an excel spreadsheet, each into it's own column. How could I do this?

If you need more information or to see more of my code, let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2024-12-10 20:41:08

尝试以下方法;

object oOpt = System.Reflection.Missing.Value; //for optional arguments
Excel.Application oXL = new Excel.Application();
Excel.Workbooks oWBs = oXL.Workbooks;
Excel.Workbook oWB = oWBs.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet oSheet = (Excel.Worksheet)oWB.ActiveSheet;

//outputRows is a List<List<object>>
int numberOfRows = outputRows.Count;
int numberOfColumns = outputRows.Max(list => list.Count);

Excel.Range oRng = 
    oSheet.get_Range("A1", oOpt)
        .get_Resize(numberOfRows, numberOfColumns);

object[,] outputArray = new object[numberOfRows, numberOfColumns];

for (int row = 0; row < numberOfRows; row++)
{
    for (int col = 0; col < outputRows[row].Count; col++)
    {
        outputArray[row, col] = outputRows[row][col];
    }
}

oRng.set_Value(oOpt, outputArray);

oXL.Visible = true;

更多详细信息,请访问 http://csharp.net-informations.com /excel/csharp-create-excel.htm

try the following;

object oOpt = System.Reflection.Missing.Value; //for optional arguments
Excel.Application oXL = new Excel.Application();
Excel.Workbooks oWBs = oXL.Workbooks;
Excel.Workbook oWB = oWBs.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet oSheet = (Excel.Worksheet)oWB.ActiveSheet;

//outputRows is a List<List<object>>
int numberOfRows = outputRows.Count;
int numberOfColumns = outputRows.Max(list => list.Count);

Excel.Range oRng = 
    oSheet.get_Range("A1", oOpt)
        .get_Resize(numberOfRows, numberOfColumns);

object[,] outputArray = new object[numberOfRows, numberOfColumns];

for (int row = 0; row < numberOfRows; row++)
{
    for (int col = 0; col < outputRows[row].Count; col++)
    {
        outputArray[row, col] = outputRows[row][col];
    }
}

oRng.set_Value(oOpt, outputArray);

oXL.Visible = true;

more details can be found at http://csharp.net-informations.com/excel/csharp-create-excel.htm

久夏青 2024-12-10 20:41:08

使用 CSV 作为文件格式,而不是 XLS。 Excel 是一种痛苦。尤其是在读取 XLS 时。有些单元格格式不正确,您有时会得到该值,但有时却得不到。即使在同一个文件中。个人经历。

Use CSV as file format, not XLS. Excel is a pain. Especially when reading from XLS. Some incorrect cell formatting and you sometimes get the value, but sometimes not. Even in same file. Personal experience.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文