I am using yii2 kartik's gridview. Have a column in which it has values like R65678, P5675,4568895,006976 etc. Now what happens is while exporting to Excel Some of the values which is having leading zeros getting truncated. So 006976 appears 6976. How can I solve this issue? Is there any way to config the excel format from the grid?
Asked
Active
Viewed 289 times
0
-
which extension are you using for exporting the data to excel from the Kartik Gridview is it https://github.com/kartik-v/yii2-export – Muhammad Omer Aslam Mar 01 '19 at 15:04
-
yes it is kartik-v/yii2-export. – user11135246 Mar 02 '19 at 07:16
1 Answers
0
This could be a problem with how excel imports data more than how the gridview is exporting it, you could try exporting the data to csv to check if the gridview is exporting it properly.
When excel imports data it tries to guess what type of value on the cell, 006976
has only digits, so excel will treat it as a number and strip the leading zeroes.
A fix for this would be to prepend a non-numeric character to the value that is being exported, that way excel will treat the value as text and preserve all characters. The Zero width non-joiner works well for that, "\u{200C}"
.
To prepend the character to the output of the gridview you can do this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'column1',
[
'attribute' => 'column2',
'value' => function ($data) {
return "\u{200C}" . $data->column2;
},
],
...
],
]); ?>

Raul Sauco
- 2,645
- 3
- 19
- 22
-
Its not working for me. It simply prepends \u{200C} with the data in both excel and grid. – user11135246 Mar 02 '19 at 05:27
-
@user11135246 the [Unicode codepoint escape syntax](https://secure.php.net/manual/migration70.new-features.php#migration70.new-features.unicode-codepoint-escape-syntax) was introduced on php7, if you are in an older version check out [this answer](https://stackoverflow.com/a/6058533/2557030). If you are using php7, make sure you use the double-quoted or heredoc style for the string, single-quoted codepoints do not get converted. – Raul Sauco Mar 03 '19 at 03:00