mvc 在view视图中直接输出rdlc报表文件(Excel、PDF、Word)
给一段代码做参考
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public ActionResult RdlcReport(string code) { LocalReport localReport = new LocalReport(); EasyMan.Dtos.ErrorInfo err = new EasyMan.Dtos.ErrorInfo(); err.IsError = false; try { var report = _reportAppService.GetReport(code, 0, false); DataTable dt = _reportAppService.GetDataTableFromCode(code, "", ref err); #region string xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <Report xmlns:rd=""http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"" xmlns:cl=""http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition"" xmlns=""http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition""> <AutoRefresh>0</AutoRefresh> <DataSources> <DataSource Name=""DummyDataSource""> <ConnectionProperties> <DataProvider>System.Data.DataSet</DataProvider> <ConnectString>/* Local Connection */</ConnectString> </ConnectionProperties> <rd:DataSourceID>31131605-8877-466c-bee6-e8f30eff39f3</rd:DataSourceID> </DataSource> </DataSources> <DataSets> <DataSet Name=""DataSet1""> <Query> <DataSourceName>DummyDataSource</DataSourceName> <CommandText>/* Local Query */</CommandText> </Query> <Fields> <Field Name=""ID""> <DataField>ID</DataField> <rd:TypeName>Decimal</rd:TypeName> </Field> <Field Name=""USER_NAME""> <DataField>USER_NAME</DataField> <rd:TypeName>String</rd:TypeName> </Field> <Field Name=""IS_LOCKOUT_ENABLED""> <DataField>IS_LOCKOUT_ENABLED</DataField> <rd:TypeName>Int16</rd:TypeName> </Field> <Field Name=""PHONE_NUMBER""> <DataField>PHONE_NUMBER</DataField> <rd:TypeName>String</rd:TypeName> </Field> <Field Name=""CREATE_TIME""> <DataField>CREATE_TIME</DataField> <rd:TypeName>DateTime</rd:TypeName> </Field> </Fields> <rd:DataSetInfo> <rd:DataSetName>DummyDataSource</rd:DataSetName> <rd:SchemaPath /> <rd:TableName>DataTable1</rd:TableName> <rd:TableAdapterFillMethod /> <rd:TableAdapterGetDataMethod /> <rd:TableAdapterName /> </rd:DataSetInfo> </DataSet> </DataSets> <ReportSections> <ReportSection> <Body> <ReportItems> <Tablix Name=""Tablix1""> <TablixBody> <TablixColumns> <TablixColumn> <Width>2.5cm</Width> </TablixColumn> <TablixColumn> <Width>2.5cm</Width> </TablixColumn> <TablixColumn> <Width>2.5cm</Width> </TablixColumn> <TablixColumn> <Width>2.5cm</Width> </TablixColumn> <TablixColumn> <Width>2.5cm</Width> </TablixColumn> </TablixColumns> <TablixRows> <TablixRow> <Height>0.6cm</Height> <TablixCells> <TablixCell> <CellContents> <Textbox Name=""Textbox1""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>ID</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>Textbox1</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> <TablixCell> <CellContents> <Textbox Name=""Textbox3""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>USER NAME</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>Textbox3</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> <TablixCell> <CellContents> <Textbox Name=""Textbox5""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>IS LOCKOUT ENABLED</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>Textbox5</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> <TablixCell> <CellContents> <Textbox Name=""Textbox7""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>PHONE NUMBER</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>Textbox7</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> <TablixCell> <CellContents> <Textbox Name=""Textbox9""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>CREATE TIME</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>Textbox9</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> </TablixCells> </TablixRow> <TablixRow> <Height>0.6cm</Height> <TablixCells> <TablixCell> <CellContents> <Textbox Name=""ID""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>=Fields!ID.Value</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>ID</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> <TablixCell> <CellContents> <Textbox Name=""USER_NAME""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>=Fields!USER_NAME.Value</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>USER_NAME</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> <TablixCell> <CellContents> <Textbox Name=""IS_LOCKOUT_ENABLED""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>=Fields!IS_LOCKOUT_ENABLED.Value</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>IS_LOCKOUT_ENABLED</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> <TablixCell> <CellContents> <Textbox Name=""PHONE_NUMBER""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>=Fields!PHONE_NUMBER.Value</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>PHONE_NUMBER</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> <TablixCell> <CellContents> <Textbox Name=""CREATE_TIME""> <CanGrow>true</CanGrow> <KeepTogether>true</KeepTogether> <Paragraphs> <Paragraph> <TextRuns> <TextRun> <Value>=Fields!CREATE_TIME.Value</Value> <Style /> </TextRun> </TextRuns> <Style /> </Paragraph> </Paragraphs> <rd:DefaultName>CREATE_TIME</rd:DefaultName> <Style> <Border> <Color>LightGrey</Color> <Style>Solid</Style> </Border> <PaddingLeft>2pt</PaddingLeft> <PaddingRight>2pt</PaddingRight> <PaddingTop>2pt</PaddingTop> <PaddingBottom>2pt</PaddingBottom> </Style> </Textbox> </CellContents> </TablixCell> </TablixCells> </TablixRow> </TablixRows> </TablixBody> <TablixColumnHierarchy> <TablixMembers> <TablixMember /> <TablixMember /> <TablixMember /> <TablixMember /> <TablixMember /> </TablixMembers> </TablixColumnHierarchy> <TablixRowHierarchy> <TablixMembers> <TablixMember> <KeepWithGroup>After</KeepWithGroup> </TablixMember> <TablixMember> <Group Name=""详细信息"" /> </TablixMember> </TablixMembers> </TablixRowHierarchy> <DataSetName>DataSet1</DataSetName> <Height>1.2cm</Height> <Width>12.5cm</Width> <Style> <Border> <Style>None</Style> </Border> </Style> </Tablix> </ReportItems> <Height>2in</Height> <Style /> </Body> <Width>6.5in</Width> <Page> <PageHeight>29.7cm</PageHeight> <PageWidth>21cm</PageWidth> <LeftMargin>2cm</LeftMargin> <RightMargin>2cm</RightMargin> <TopMargin>2cm</TopMargin> <BottomMargin>2cm</BottomMargin> <ColumnSpacing>0.13cm</ColumnSpacing> <Style /> </Page> </ReportSection> </ReportSections> <rd:ReportUnitType>Cm</rd:ReportUnitType> <rd:ReportID>74c3f382-2b1e-4b36-aa0f-860307cf4cb1</rd:ReportID> </Report>"; #endregion localReport.LoadReportDefinition(GenerateRdlc(xml)); ReportDataSource reportDataSource = new ReportDataSource("DataSet1", dt); localReport.DataSources.Add(reportDataSource); localReport.Refresh(); Warning[] warnings; string[] streamids; string mimeType; string encoding; string extension; string deviceInfo = "<DeviceInfo>" + "<SimplePageHeaders>True</SimplePageHeaders>" + "</DeviceInfo>"; byte[] bytes = localReport.Render("Excel", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings); return new ReportsResult(bytes, mimeType); return File(bytes, mimeType); } catch (Exception ex) { throw new Exception(ex.Message); } }
其中xml变量为rdlc配置xml
一些示例网站:
基于MVC4+EasyUI的Web开发框架经验总结(15)--在MVC项目中使用RDLC报表
ReportViewer报表导出成Excel,Image,PDF---LocalReport.Render