Search code examples
export-to-csvreact-typescriptpapaparse

Parse CSV string auto convert to number issue


I am trying to convert JSON data into CSV using papa parse. In my JSON I have a field article id which is a string type but when showing in CSV it automatically converts it into a number which creates the issue that my article is big in number but the column size of Excel is small which my number which should be shown like this 490750661 is showing like this 4.9E+08 . The same goes for my date field also. The number is indicated correctly when I manually increase the width of the column.

Default view
enter image description here

After manually increasing the width

enter image description here

I tried explicitly specifying those two fields as numbers but it still didn't work.

const flatData = dataJSON.flatMap((order: any) =>
        order.line_items.map((item: any) => ({
          order_id: order._id,
          article_id: item.article_code.toString(),
          quantity: item.quantity,
          merchant_id: order.merchant_id,
          date: dayjs(order.createdAt).format("YYYY-MM-DD").toString(),
        }))
      );

Solution

  • I thought of another alternative giving space between the numbers like we used to see phone numbers in forms and Excels i.e. 12345 5678 so this will also add a professional look. enter image description here