Click here to Skip to main content
15,920,508 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have a data in the below format, i have to convert this data(1) into the excel as mention next below(2)


(1)
Party:DR NAYEEM ASHRAF               ,           
----------------------
SMBCS  BASICS TABS               10 s             24.0      0.0      1627.20 
SMMNT  MECOZEN -NT               10s              10.0      0.0      1064.30 
--------------------------------------------------------------------------------
Total                                                                2691.50 
--------------------------------------------------------------------------------
Party:DR S A HAKEEM                  ,           
-------------------
SMASP  ALTO-SP                   10 s            100.0      0.0      4286.00 
--------------------------------------------------------------------------------
Total                                                                4286.00 
--------------------------------------------------------------------------------
Party:FAITH CHEMIST & DRUGGIST       ,           
------------------------------
SMBJS  BASICS JUNIOR SYP         200ml            10.0      0.0       867.80 
SMD6   DZCOT-6                   10               20.0      0.0      1128.60 
SMMNT  MECOZEN -NT               10s              10.0      0.0      1064.30 
SMMPG  MECOZEN PG CAPS           10 s             20.0      0.0      1714.20 
SMPPD  PROPLEX-DHA-200GM         200gm             3.0      0.0       337.50 
SMSZL  SAZITH 250 TABS  (SAIN)   3,s              50.0      0.0      1018.00 
SMSZH  SAZITH 500 TABS  (SAIN)   3,s              80.0      0.0      3200.00 
SMSCH  SUCRAGEL  SUSP  200 ML    200ml            20.0      0.0      1971.40 
SMSCO  SUCRAGEL-O SUSP           100ml             3.0      0.0       252.87 
--------------------------------------------------------------------------------
Total                                                               11554.67 
--------------------------------------------------------------------------------



(2)

DR NAYEEM ASHRAF  	  SMBCS  BASICS TABS           10 s   24.0    0.0      1627.20 
DR NAYEEM ASHRAF  	  SMMNT  MECOZEN -NT           10s    10.0    0.0      1064.30 
DR S A HAKEEM  		  SMASP  ALTO-SP               10 s  100.0    0.0      4286.00 
FAITH CHEMIST & DRUGGIST  SMBJS  BASICS JUNIOR SYP    200ml   10.0    0.0       867.80 
FAITH CHEMIST & DRUGGIST  SMD6   DZCOT-6               10     20.0    0.0      1128.60 
FAITH CHEMIST & DRUGGIST  SMMNT  MECOZEN -NT           10s    10.0    0.0      1064.30 
FAITH CHEMIST & DRUGGIST  SMMPG  MECOZEN PG CAPS       10 s   20.0    0.0      1714.20 
FAITH CHEMIST & DRUGGIST  SMPPD  PROPLEX-DHA-200GM     200gm   3.0    0.0       337.50 
FAITH CHEMIST & DRUGGIST  SMSZL  SAZITH 250 TABS(SAIN) 3,s    50.0    0.0      1018.00 
FAITH CHEMIST & DRUGGIST  SMSZH  SAZITH 500 TABS(SAIN) 3,s    80.0    0.0      3200.00 
FAITH CHEMIST & DRUGGIST  SMSCH  SUCRAGEL  SUSP        200ml  20.0    0.0      1971.40 
FAITH CHEMIST & DRUGGIST  SMSCO  SUCRAGEL-O SUSP       100ml  3.0     0.0       252.87


What I have tried:

i tried different codes, where i got like "consolidated format as in excel". I am fresher please let me know.

Thank you in advance
Posted
Updated 28-Dec-17 3:06am
v2
Comments
OriginalGriff 28-Dec-17 8:39am    
And?
What have you tried?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.

1 solution

refer this to get DataTable from the input text
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPTemp
{

    class temp
    {
        static void Main(string[] args)
        {
            DataTable dtFinal = new DataTable();
            dtFinal.Columns.Add("Party");
            dtFinal.Columns.Add("Type");
            dtFinal.Columns.Add("Tablet");
            dtFinal.Columns.Add("Value1");
            dtFinal.Columns.Add("Value2");
            dtFinal.Columns.Add("Value3");
            dtFinal.Columns.Add("Value4");


            string text = File.ReadAllText(@"D:\Projects\CPTemp\CPTemp\TextFile1.txt"); // Read the text from your file
            var parties = text.Split(new string[] { "Party:" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string party in parties)
            {
                DataRow row = dtFinal.NewRow();

                var lines = party.Split(new string[] { "----------------------" }, StringSplitOptions.RemoveEmptyEntries);
                string name = lines[0].Trim().Trim(' ', ',');
                string content = lines[1];
                string[] items = content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string _item in items)
                {
                    string item = _item.Trim('-').Trim();
                    if (item.Length > 0 && !item.StartsWith("Total"))
                    {

                        string[] cols = item.Split(new string[] { "   " }, StringSplitOptions.RemoveEmptyEntries);
                        var parts = cols[0].Split(' ');
                        string type = parts[0];
                        string tablet;
                        if (parts.Length == 1)
                            tablet = cols[1];
                        else
                            tablet = string.Join(" ", parts.Skip(1));
                        string value1 = (cols.Length == 5) ? cols[1] : cols[0];

                        dtFinal.Rows.Add(name, type, tablet, value1, cols[2], cols[3], cols[4]);
                    }
                }
            } 

        }
    }
}


use ClosedXML [^] to export the datatable to excel.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900