The House voted yesterday, 345 to 23, to extend federal revenue sharing to the nation's cities and towns for three years and fended off a barrage of efforts by some Democrats to cut state governments out of the popular multibillion-dollar program.

State government emerged from the fray somewhat battered, however. They would be excluded from receiving the "no strings" federal dollars in the current fiscal year, as the White House and Congress seek to hold down federal spending, but would be authorized to receive the funds again for the following two years. They would be eligible for revenue-sharing dollars then only if they turn back to the federal treasury other federal funds earmarked for specific programs such as transportation, housing and education.

The effort to take the states out of the 8-year-old revenue-sharing program was led by a group of Democrats who depicted the states as being fat and arrogant, sitting on huge surpluses while lobbying for more federal funds and at the same time lecturing Congress about the need for a balanced federal budget.

"They've got money in their pocket and don't really need it and don't know what to do with it, yet they scream, they cry, they write letters," said Rep. Jack Brooks (D-Tex.), chairman of the Government Operations Commitee, which handles revenue-sharing legislation. Brooks saw the committee's bill virtually rewritten on the floor yesterday.

A survey by the National Governors Association last January estimated that the 50 states would have surpluses totaling more than was concentrated in energy-producing states such as Texas, Alaska and California. As the recession worsened, several states revised their revenue estimates downward, and some, including New York and Oregon, are now expecting deficts.

The governors association has called for restraint on federal spending, and 30 state legislatures have passed resolutions calling for a constitutional amendment requirng a balanced federal budget -- actions that irritated Brooks and some of his allies in Congress.

In the debate and parliamentary manuevering on the House floor yesterday, Brooks and his allies seemed to take pleasure in chiding Republicans' support for the states, arguing that cutting them out was the fiscally responsible position.

Rep. Elliott H. Levitas (D-Ga.) said he had read the election returns and was heeding the message of the voters to restrain spending. "We're in a put-up or shut-up situation," he said.

The tactic clearly made at least some Republican members uncomfortable, and, while it was not enough to exclude the states from the revenue-sharing program, it seemed to help Levitas get approval, on a voice vote, of a "tit for tat" amendment requiring the states to return categorical funds, dollar-for-dollar for the amount they receive in revenue sharing.

Under the bill, 11,000 cities and other local governments would get $4.6 billion a year in revenue-sharing funds for three years. State governments would be authorized to receive $2.3 billion in the 1982 and 1983 fiscal years, but only if Congress appropriates those funds.

At the heart of yesterday's debate were differing philosophies on federal spending that are certain to be hard again after President-elect Ronald Reagan takes office Jan. 20.

Brooks is an ardent foe of allowing states and local governments to spend federal dollars as they please, Reagan favors such an approach, arguing that too much of the federal money sent to local governments comes with too many rules and other mandates attached by Congress and federal agencies.

Rep. Herbert Harris (D-va.) was the only Washington area representative voting with Brooks to exclude the states, on an amendment that was defeated 158 to 215. Harris also was alone among Washington Area members of the House who voted against the full bill.

A similar bill that would extend revenue sharing for five years is pending in the Senate. It also includes additional funds for local governments hit hardest by the recession. Brooks' committee had earmarked an additional $1 billion in the House bill for the same purpose, but it was thrown out in the complex maneuvering on the floor. If the Senate bill passes, differences will have to be worked out in a conference committee.