Poland today ordered the expulsion of an American correspondent for United Press International in Warsaw, Ruth Gruber, charging that she had abused her position as a foreign correspondent.

The government said two rolls of film containing photos of Poland's military defenses were sent to her on a train from Gdansk. Gruber, who never actually saw the film, denied knowing what it might have contained when she authorized its pickup by a UPI office assistant at a Warsaw train station.

Poland said a strong protest was registered with the U.S. government. An embassy spokesman, characterizing Gruber's expulsion as a grave matter, said he expected the Reagan administration would retaliate. She was held for 23 hours incommunicado and was released following a protest from the embassy.

It was the second expulsion of an accredited western reporter this month. A British Broadcasting Corp. correspondent was ousted last week in response to a program on Poland's martial law.

"We are greatly relieved that Miss Gruber has been freed," H.L. Stevenson, UPI editor-in-chief, said in New York, adding: "We are distressed" by the order to expel her from Poland, "where she has done a superb job of accurate and impartial reporting during the past two years. We intend to continue providing that type of coverage despite this obvious attempt by the government to intimidate all reporters in Warsaw."

Polish television accused Gruber of contacts with outlawed dissidents. An official communique on Gruber's expulsion said that military authorities initially had opened legal proceedings involving espionage charges but "appropriate authorities" had discontinued the proceedings, "concluding that she had no criminal intentions."

"These statements contradict themselves," said Gruber, calm after her release this afternoon. She told reporters she had not been informed of any charges against her while she was questioned at police headquarters. The interrogation, which she said lasted several hours last night and involved her signing a statement giving her version of the train package story, dealt extensively with how she operated as a correspondent in Poland.

Gruber, 33, said she had assumed the package was from a free-lance photographer in Gdansk who routinely does work for the agency. The photographer, who was also later questioned by police, has denied placing the call, Tuesday morning, to which Gruber responded, and UPI staffers here have been unable to identify the caller.

Gruber's office assistant, Anna Olszewski, was detained as she tried to pick up the film shipment at the train station. Gruber was released this afternoon but Olszewski was reported by UPI still in detention at Warsaw police headquarters.

Polish television featured the expulsion story on the evening news, portraying the incident as another example of anti-Polish activities by western news agencies. Earlier this week, the government issued a booklet criticizing a number of western reporters and their publications by name for what was presented as distorted coverage of Poland.

More serious to news-gathering operations here, authorities are refusing to renew the work permits of nine Poles who serve as assistants for western news organizations in Warsaw.

A Polish train conductor, Leokadia Potocka, told Polish TV she had been approached at the station in the northern port town of Gdynia, near Gdansk, on Tuesday and asked to carry two envelopes with her to Warsaw. One of the envelopes, the government says, was addressed to Gruber and included the two rolls of film.

Olszewski, also appearing on television but with her back to the camera, recounted a phone call Tuesday morning to the UPI office from a man who said he was phoning from Gdansk and advising that the envelopes be retrieved from the woman conductor when her train arrived in Warsaw. Olszewski said she checked with Gruber, who was at home, and she instructed that the envelopes be picked up.

The television showed pictures of Olszewski receiving a package from conductor Potocka on a platform at Warsaw's Western Station. A TV announcer said the police had been tipped in advance about the exchange.

The broadcast included some still photos identified as those in the envelope to Gruber, but they were not clear. The announcer identified the pictures as showing a military headquarters and port equipment.

The announcer added that this had not been Gruber's first search for information about Polish military affairs. He cited a message from UPI's New York office to Gruber which he said was sent in August 1982 instructing her to gather information on the military and on local reaction to the war in Lebanon and the Falklands war. Gruber said she had no recollection of the memo, nor did she recognize the name of the UPI editor who Polish television said had sent the cable.

"We do not mention," concluded the announcer, "the wide contacts Gruber had with the outlawed dissident groups Committee for Workers' Self-Defense and Confederation of Independent Poland."

Gruber said, "As far as I'm concerned, this is all a part of the escalating activity against the western press. They knew everything about this package except who the man was who handed it over first, and they don't seem to be trying very hard to find that out."

She said she had no complaints about her treatment at the headquarters, where she spent last night in a basement cell, "except for the weak coffee and being kept incommunicado."