简体   繁体   中英

Export Data to multiple Excel cells from one RichTextBox C#

I am receiving data from a COM device into a RichTextBox. More than one result is displayed under each other in the RichTextBox as I receive data. I am able to export the data to Excel but it does not put every result in its own cell under each other. Instead it exports all the data from the RichTextBox into one cell. Can you please point me in the right direction to accomplish my task? Using WinForms C# Please see attached images.

InterFace

Current Excel sheet being Created

private void button4_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

        if (xlApp == null)
        {
            MessageBox.Show("Excel is not properly installed!!");
            return;
        }


        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

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

        xlWorkSheet.Cells[1, 1] = "Date";
        xlWorkSheet.Cells[1, 2] = "Batch No";
        xlWorkSheet.Cells[1, 3] = "Totale Sakke";
        xlWorkSheet.Cells[1, 4] = "Pallet Gewig";
        xlWorkSheet.Cells[1, 5] = "Tipe Sak";
        xlWorkSheet.Cells[1, 6] = "Kultivar";
        xlWorkSheet.Cells[1, 7] = "Gewig p/sak";
        xlWorkSheet.Cells[2, 1] = DateTime.Today;
        xlWorkSheet.Cells[2, 2] = txtBatch.Text;
        xlWorkSheet.Cells[2, 3] = txtTotaleSakke.Text;
        xlWorkSheet.Cells[2, 4] = txtPalletWeight.Text;
        xlWorkSheet.Cells[2, 5] = cmbTipeSak.Text;
        xlWorkSheet.Cells[2, 6] = cmbKultivar.Text;
        xlWorkSheet.Cells[2, 7] = txbWeightList.Text;
        xlWorkSheet.Columns.AutoFit();
        xlWorkSheet.Rows.AutoFit();


        xlWorkBook.SaveAs("C:\\Users\\Huiz\\Documents\\Lewering Excel\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        Marshal.ReleaseComObject(xlWorkSheet);
        Marshal.ReleaseComObject(xlWorkBook);
        Marshal.ReleaseComObject(xlApp);

        MessageBox.Show("Excel file created , you can find the file C:\\csharp-Excel.xls");
sting[] stringList= stringtxbWeightList.Text.Split(' ');
for (int row=2;row<(stringList.Length+2);row++)
{
xlWorkSheet.Cells[row, 7]=stringList[row-2];


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM