欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

读取excel文件,只能读取A列

程序员文章站 2022-04-22 14:56:56
...
function import_excel($filePath){  $txt=array();      $PHPReader = new PHPExcel_Reader_Excel2007();  if(!$PHPReader->canRead($filePath)){      $PHPReader = new PHPExcel_Reader_Excel5();      if(!$PHPReader->canRead($filePath)){          echo 'no Excel';          return ;      }  }   //建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件  $PHPExcel = $PHPReader->load($filePath);    /**读取excel文件中的第一个工作表*/  $currentSheet = $PHPExcel->getSheet(0);  /**取得最大的列号*/  $allColumn = $currentSheet->getHighestColumn();  /**取得一共有多少行*/  $allRow = $currentSheet->getHighestRow();    echo "allcolumn:".$allColumn."
"; echo "allRow:".$allRow."
";//循环读取每个单元格的内容。注意行从1开始,列从A开始 for($rowIndex=1;$rowIndexgetCell($addr)->getValue(); $txt[$rowIndex][]=$cell; echo $cell,","; } echo "
"; } return $txt; }


只能读取A列的内容,后面的都读不到,网上的示例都是这样的
是不是 for($colIndex='A';$colIndex

回复讨论(解决方案)

echo "allcolumn:".$allColumn."
";
这个输出的是什么?

for($colIndex='A';$colIndex
$colIndex='A';
$colIndex++

A是字符,?行++ 只??示是1
所以每次都是1

改?
for($colIndex=0;$colIndex ??

请不要误导,楼主的代码并无大问题

for($i='A'; $i!='AA'; $i++) echo $i;
ABCDEFGHIJKLMNOPQRSTUVWXYZ



for($colIndex='A';$colIndex
$colIndex='A';
$colIndex++

A是字符,?行++ 只??示是1
所以每次都是1

改?
for($colIndex=0;$colIndex ??

请不要误导,楼主的代码并无大问题

for($i='A'; $i!='AA'; $i++) echo $i;
ABCDEFGHIJKLMNOPQRSTUVWXYZ




for($colIndex='A';$colIndex
$colIndex='A';
$colIndex++

A是字符,?行++ 只??示是1
所以每次都是1

改?
for($colIndex=0;$colIndex ??



明白,如果????就不在??了。

楼主的代码输出什么?

echo "allcolumn:".$allColumn."
";
输出的是AD

不能改成数字 吧??

版主大大:
for($i='A'; $i!='AA'; $i++) echo $i;
$i!='AA',请问最后这个AA怎么处理啊,现在最后一列没读取到

我先
$allColumn++
就可以读取到最后一行了。

还有一个问题,我现在想输出文本

for($rowIndex=1;$rowIndex for($colIndex='A';$colIndex!=$allColumn;$colIndex++){

$addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
$txt.=$cell."\t";
}
$txt.="\n";
}

结果:
Transaction Date Order Id Transaction ID Order State SKU Product Product Link Variation Price (each) Cost (each) Shipping (each) Shipping Cost (each) Quantity Total Cost Shipped on Provider Tracking Shipping Address Name First Name Last Name Street Address 1 Street Address 2 City State Zipcode Country Last Updated Phone Number Country Code

输出的都是空格

你在浏览器中观看时 \t、\n 都表现为空格,因为这些都是文本方式的控制符
你要在浏览器中按文本格式显示的话,需将内容放在预格式化标签(pre、xmp)中

$file=import_excel("../upload/123.xlsx");
$order_array=explode("\n",$file);
foreach($order_array as $value){
$aa=explode(' ',$value);
print_r($aa);
}

我这个输出结果都不对
Array ( [0] => Transaction Date [1] => [2] => Order Id [3] => [4] => Transaction ID [5] => [6] => Order State [7] => [8] => SKU [9] => [10] => Product [11] => [12] => Product Link [13] => [14] => Variation [15] => [16] => Price (each) [17] => [18] => Cost (each) [19] => [20] => Shipping (each) [21] => [22] => Shipping Cost (each) [23] => [24] => Quantity [25] => [26] => Total Cost [27] => [28] => Shipped on [29] => [30] => Provider [31] => [32] => Tracking [33] => [34] => Shipping Address [35] => [36] => Name [37] => [38] => First Name [39] => [40] => Last Name [41] => [42] => Street Address 1 [43] => [44] => Street Address 2 [45] => [46] => City [47] => [48] => State [49] => [50] => Zipcode [51] => [52] => Country [53] => [54] => Last Updated [55] => [56] => Phone Number [57] => [58] => Country Code [59] => [60] => )


请问怎么去除字符串两边的空格,制表符这些???

$file=import_excel("../upload/123.xlsx");
后 ptint_r($file); 看看是什么

$file就是输出的文本

Transaction Date Order Id Transaction ID Order State SKU Product Product Link Variation Price (each) Cost (each) Shipping (each) Shipping Cost (each) Quantity Total Cost Shipped on Provider Tracking Shipping Address Name First Name Last Name Street Address 1 Street Address 2 City State Zipcode Country Last Updated Phone Number Country Code 04-12-2015 552aabb65633ba0be4cbf1b2 552aabb3df5a100cc01f40d4 APPROVED 6402-BK Neewer 3.5mm Jack Retractable Auxiliary Cord for iPod/MP3 Players/Cell Phones-6402-BK https://www.wish.com/c/53f179f21c105e50bba1b5b8 n/a $1.99 $1.69 $0.00 $0.00 1 $1.69 Loay Alsalih 7919 S 82 nd. Ct Apt. 3 S Justice Illinois 60458 United States 7085487881 Loay Alsalih Loay Alsalih 7919 S 82 nd. Ct Apt. 3 S Justice IL 60458 United States 04-13-2015 7085487881 US 04-12-2015 552abba85633ba0be6cbf1ab 552abba8b2e2590cc84f7c59 APPROVED 6452-00 Extendable Handheld Selfie Stick Monopod Holder For iPhone Samsung Phones https://www.wish.com/c/53fefa589020ee2b46eb8ab0 color: black 9.99 $8.49 $0.00 $0.00 1 $8.49 Jonas Pettersson Skaraborgsvägen 3 ROSVIK None 94533 Sweden 070 558 71 60 Jonas Pettersson Jonas Pettersson Skaraborgsvägen 3 ROSVIK 94533 Sweden 04-13-2015 070 558 71 60 SE 04-12-2015 552ac7d3bd74230c00e30781 552ac7d2be48440cc1704ef2 APPROVED 6634-00 BLA-7 Fheimin® 2014 new spring high-heeled shoes wedding shoes platform fashion women shoes pumps red bottom high heels 14cm https://www.wish.com/c/53e05f62d911394475ed77fe size: 7, color: black $24.99 $21.24 $4.00 $3.40 1 $24.64 Jamie 107 e overton st Overton Texas 75684 United States 9038060856 Jamie Jami Jamie 107 e overton st Overton TX 75684 United States 04-13-2015 9038060856 US 04-12-2015 552acffdcaf0e00beb89afea 552acff273f6810cb5d34baa APPROVED 6007-3M Fheimin® Extra Long 3m 10ft Long USB Data Charger Sync Cable for iPhone 5 5G iPad Mini iPod Touch 5 5G iPod Nano 7 7G-6007-3M https://www.wish.com/c/53eb0924104dae3decadab9a n/a $3.99 $3.39 $0.00 $0.00 1 $3.39 Nuno Cardoso Fragueiro Molnvädersgatan 30 Lgh 1201 GÖTEBORG None 41835 Sweden 076 279 57 23 Nuno Cardoso Fragueiro Nuno Cardoso Fragueiro Molnvädersgatan 30 Lgh 1201 GÖTEBORG 41835 Sweden 04-13-2015 076 279 57 23 SE 04-12-2015 552ad57ce508974e53e20748 552ad5795f4d680cb2ab3d08 APPROVED 6062-00 Fheimin® TSdena mobile multi-functional digital scale (scales) 0.01g units / up to 100g pocket scale HEM-KDSP01001S-S (japan import)-6062-00 https://www.wish.com/c/53eb1df81f506345376506d7 n/a $9.99 $8.49 $0.00 $0.00 1 $8.49 Shanne Botizan 931 National road Wheeling West Virginia 26003 United States 3043127670 Shanne Botizan Shanne Botizan 931 National road Wheeling WV 26003 United States 04-13-2015 3043127670 US 04-12-2015 552ae00067bd420bf1830c8f 552ae0001f1b330cc784fb50 APPROVED 6105-WH Mini Charger MICRO CHARGEUR ALLUME CIGARE USB (Shipping to France Only) https://www.wish.com/c/540d30087f086e6cbdc659a9 n/a $1.99 $1.69 $1.00 $0.85 1 $2.54 Csokany ana 41bis rue Auguste renoir Appartement A20 31200 Toulouse 31200 France 0662214649 Csokany ana Csokany ana 41bis rue Auguste renoir Appartement A20 31200 Toulouse 31200 France 04-13-2015 0662214649 FR 04-12-2015 552aff6a5fc2ef0bdc57bd2e 552aff6a0bacd50cc3ae7165 APPROVED 6106-MX KIT CARICABATTERIE per iPhone 3G 3GS 4 4S S caricatore da MURO AUTO/macchina + CAVO USB (shipping to Italy Only) https://www.wish.com/c/540d58677a9eb43980922bd3 n/a $4.99 $4.24 $4.00 $3.40 1 $7.64 Aggoune sahra 1 rue de mon desert Nancy None 54000 France 0761632175 Aggoune sahra Aggoune sahra 1 rue de mon desert Nancy 54000 France 04-13-2015 0761632175 FR 04-13-2015 552b08afbd74230c0de30b88 552b08ad1e260e0cac3bf3ee APPROVED 6452-BL Extendable Handheld Selfie Stick Monopod Holder For iPhone Samsung Phones https://www.wish.com/c/53fefa589020ee2b46eb8ab0 color: blue $9.99 $8.49 $0.00 $0.00 1 $8.49 Gabriel sanchez 2118 NE 10 Pl Ocala Florida 34470 United States 3525029526 Gabriel sanchez Gabriel sanchez 2118 NE 10 Pl Ocala FL 34470 United States 04-13-2015 3525029526 US 04-13-2015 552b10bbbdc6040bd2a84eac 552b10bb73f6810cb2d3690c APPROVED 6634-00 BLA-8.5 Fheimin® 2014 new spring high-heeled shoes wedding shoes platform fashion women shoes pumps red bottom high heels 14cm https://www.wish.com/c/53e05f62d911394475ed77fe size: 8.5, color: black $24.99 $21.24 $4.00 $3.40 1 $24.64 Destanie Finley 6200 college ave Snyder Texas 79549 United States 3256426040 Destanie Finley Destanie Finley 6200 college ave Snyder TX 79549 United States 04-13-2015 3256426040 US

搞定了
加个$cell = str_replace("\n", '', $cell); //清除换行符