[英]Get each line from a richtextbox and export to excel cells using c#
[英]Export Data to multiple Excel cells from one RichTextBox C#
我正在從 COM 設備接收數據到 RichTextBox。 當我收到數據時,在 RichTextBox 中顯示了多個結果。 我能夠將數據導出到 Excel,但它不會將每個結果都放在各自的單元格中。 相反,它會將 RichTextBox 中的所有數據導出到一個單元格中。 你能給我指出正確的方向來完成我的任務嗎? 使用 WinForms C# 請參閱附圖。
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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.