Asp.net gridview图像优秀 [英] Asp.net gridview images to excel

查看:61
本文介绍了Asp.net gridview图像优秀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Microsoft.office.intreop导出然后在我的图像列System.Drawing.image即将到来或当我完成Response.Header时,我想使用c#将我的gridview导出到excel然后我的图像即将到来,但是如果图像尺寸超过我的图像就会出现。





我尝试了什么:



< pre> protected void Unnamed_Click(object sender,EventArgs e)
{
System.Data.DataTable dts = new System.Data.DataTable();
dts.Columns.Add(UserId,typeof(Int32));
dts.Columns.Add(UserName,typeof(string));
dts.Columns.Add(Education,typeof(string));
dts.Columns.Add(Imagepath,typeof(Image));
// context.Response.BinaryWrite((Byte [])[ismg。]);
dts.Rows.Add(1,Suresh Dasari,B.Tech,GetImageFromFile(〜/ images / EC.png));
dts.Rows.Add(2,Suresh Dasarsi,B.Tech,GetImageFromFile(〜/ images / EC.png));
Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

尝试
{
worksheet = workbook.ActiveSheet;

worksheet.Name =ExportedFromDatGrid;

int cellRowIndex = 1;
int cellColumnIndex = 1;
////遍历每一行并从每列读取值。
for(int i = 0; i< dts.Rows.Count; i ++)
{
for(int j = 0; j< dts.Columns.Count; j ++)
{
//// Excel索引从1,1开始。由于第一行将具有列标题,因此添加条件检查。
if(cellRowIndex == 1)
{
worksheet.Cells [cellRowIndex,cellColumnIndex] = dts.Columns [j] .ColumnName;
worksheet.Cells [cellRowIndex,cellColumnIndex] .Font.FontStyle = FontStyle.Bold;
}
else
{
worksheet.Cells [cellRowIndex,cellColumnIndex] = dts.Rows [i] [j] .ToString();
}

cellColumnIndex ++;
}

cellColumnIndex = 1;
cellRowIndex ++;
}

worksheet.Columns.Horizo​​ntalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
worksheet.Columns.AutoFit();

////获取要从用户保存的Excel的位置和文件名。
workbook.SaveAs(ss.xls,XlFileFormat.xlExcel12,System.Reflection.Missing.Value,System.Reflection.Missing.Value,false,false,XlSaveAsAccessMode.xlShared,XlSaveConflictResolution.xlLocalSessionChanges,false,System。 Reflection.Missing.Value,System.Reflection.Missing.Value,false);
}
catch(System.Exception ex)
{
// MessageBox.Show(ex.Message,Error,MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1 );
}
最后
{
excel.Quit();
workbook = null;
excel = null;
}
}

private static Image GetImageFromFile(string fileName)
{
string path = fileName;
//检查光盘中是否存在文件
if(File.Exists(path))
{
Image image = new Image();
image.Url = path;
返回图片;
}
else
返回null;
}

解决方案

使用以下代码



C#



受保护的字符串GetUrl(字符串图像路径)

{

string [] splits = Request.Url.AbsoluteUri.Split('/');

if(splits.Length> = 2)

{

string url = splits [0] +//;

for(int i = 2; i< splits.Length - 1; i ++)

{

url + = splits [i] ;

url + =/;

}

return url + imagepath;

}

返回imagepath;

}





< asp:GridView ID =GridView1runat = serverAutoGenerateColumns =false

字体名称=Arial>

< Columns>

< asp:BoundField DataField =IDHeaderText =ID

ItemStyle-Height =150/>

< asp:BoundField DataField =FileNameHeaderText =Image Name

ItemStyle-Height =150/>

< asp:TemplateField ItemStyle-Height =150ItemStyle-Width =170>

< ItemTemplate>

< asp:图片ID =Image1runat =server

ImageUrl ='<%#Eval(FilePath,GetUrl({0} ))%>'/>

< / ItemTemplate>

< / asp:TemplateField>

< / Columns>

< / asp:GridView>



导出到Excel代码



 private void Excel_Export()

{

Response.Clear();

Response.Buffer = true;

Response.AddHeader(content-disposition,

attachment; filename = GridViewExport.xls);

Response.Charset =;

Response.ContentType =application / vnd.ms-excel;

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;

GridView1.DataBind();

for(int i = 0; i< GridView1.Rows.Count; i ++)

{

GridViewRow row = GridView1.Rows [一世];

//将文本样式应用于每一行

row.Attributes.Add(class,textmode);

}

GridView1.RenderControl(hw);



//样式将数字格式化为字符串

string style = @< style> .textmode {mso-number-format: \ @;}< / style>;

Response.Write(style);

Response.Output.Write(sw.ToString());

Response.Flush();

Response.End();

}





源代码链接:

https:// www .aspsnippets.com /用品/导出-GridView控件,用图像对字的Excel和PDF的格式功能于ASP.Net.aspx

I want to export my gridview to excel using c# when i am exporting with Microsoft.office.intreop then in my image column System.Drawing.image is coming or when i have done with Response.Header then my images are coming but if image size is more than my image is coming out of row.



What I have tried:

<pre>   protected void Unnamed_Click(object sender, EventArgs e)
        {
            System.Data.DataTable dts = new System.Data.DataTable();
            dts.Columns.Add("UserId", typeof(Int32));
            dts.Columns.Add("UserName", typeof(string));
            dts.Columns.Add("Education", typeof(string));
            dts.Columns.Add("Imagepath", typeof(Image));
          //  context.Response.BinaryWrite((Byte[])[ismg.]);
            dts.Rows.Add(1, "Suresh Dasari", "B.Tech", GetImageFromFile("~/images/EC.png"));
            dts.Rows.Add(2, "Suresh Dasarsi", "B.Tech", GetImageFromFile("~/images/EC.png"));
            Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

            try
            {
                worksheet = workbook.ActiveSheet;

                worksheet.Name = "ExportedFromDatGrid";

                int cellRowIndex = 1;
                int cellColumnIndex = 1;
                ////Loop through each row and read value from each column. 
                for (int i = 0; i < dts.Rows.Count; i++)
                {
                    for (int j = 0; j < dts.Columns.Count; j++)
                    {
                        //// Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check. 
                        if (cellRowIndex == 1)
                        {
                            worksheet.Cells[cellRowIndex, cellColumnIndex] =dts.Columns[j].ColumnName;
                            worksheet.Cells[cellRowIndex, cellColumnIndex].Font.FontStyle = FontStyle.Bold;
                        }
                        else
                        {
                            worksheet.Cells[cellRowIndex, cellColumnIndex] =dts.Rows[i][j].ToString();
                        }

                        cellColumnIndex++;
                    }

                    cellColumnIndex = 1;
                    cellRowIndex++;
                }

                worksheet.Columns.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
                worksheet.Columns.AutoFit();

                ////Getting the location and file name of the excel to save from user. 
                workbook.SaveAs("ss.xls", XlFileFormat.xlExcel12, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, XlSaveAsAccessMode.xlShared, XlSaveConflictResolution.xlLocalSessionChanges, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false);
            }
            catch (System.Exception ex)
            {
               // MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
            }
            finally
            {
                excel.Quit();
                workbook = null;
                excel = null;
            }
        }

 private static Image GetImageFromFile(string fileName)
        {
            string path = fileName;
            //check the existence of the file in disc
            if (File.Exists(path))
            {
                Image image =new  Image();
                image.Url = path;
                return image;
            }
            else
                return null;
        }

解决方案

Use the Below Code

C#

protected  string GetUrl(string imagepath)

{

    string[] splits = Request.Url.AbsoluteUri.Split('/');

    if (splits.Length  >= 2)

    {

        string url = splits[0] + "//";

        for (int i = 2; i < splits.Length - 1; i++)

        {

            url += splits[i];

            url += "/";

        }

        return url +  imagepath;

    }

    return imagepath;

}



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"

Font-Names = "Arial" >

<Columns>

    <asp:BoundField DataField = "ID" HeaderText = "ID"

     ItemStyle-Height = "150" />

    <asp:BoundField DataField = "FileName" HeaderText = "Image Name"

     ItemStyle-Height = "150"/>

    <asp:TemplateField  ItemStyle-Height = "150" ItemStyle-Width = "170">

        <ItemTemplate>

            <asp:Image ID="Image1" runat="server"

             ImageUrl = '<%# Eval("FilePath", GetUrl("{0}")) %>' />

        </ItemTemplate>

    </asp:TemplateField>

</Columns>

</asp:GridView>


Export to Excel Code

private void Excel_Export()

{

    Response.Clear();

    Response.Buffer = true;

    Response.AddHeader("content-disposition",

     "attachment;filename=GridViewExport.xls");

    Response.Charset = "";

    Response.ContentType = "application/vnd.ms-excel";

    StringWriter sw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;

    GridView1.DataBind();

    for (int i = 0; i < GridView1.Rows.Count; i++)

    {

        GridViewRow row = GridView1.Rows[i];

        //Apply text style to each Row

        row.Attributes.Add("class", "textmode");

    }

    GridView1.RenderControl(hw);

 

    //style to format numbers to string

    string style = @"<style> .textmode { mso-number-format:\@; } </style>";

    Response.Write(style);

    Response.Output.Write(sw.ToString());

    Response.Flush();

    Response.End();

}



Link for Source Code:
https://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx


这篇关于Asp.net gridview图像优秀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆