乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > C#winform 如何导出一定格式的excel啊?

C#winform 如何导出一定格式的excel啊?

作者:乔山办公网日期:

返回目录:excel表格制作


你是要从excel中导入数据到winform吗?如果是这样,7a686964616fe4b893e5b19e332可以这样:引用office11.0组件后, Microsoft.Office.Interop.Excel.Application application; //这是一个客户端
Microsoft.Office.Interop.Excel.Workbooks workbooks; //所有工作薄
Microsoft.Office.Interop.Excel.Worksheet worksheet;//工作表
Microsoft.Office.Interop.Excel.Workbook workbook; //所用到的工作表 void IsRunEX(){ OpenFileDialog openfilediaglog = new OpenFileDialog();
openfilediaglog.Filter = "xls文件|*.xls";
if (openfilediaglog.ShowDialog() == DialogResult.OK)
{
FieldName = openfilediaglog.FileName;
application = new Microsoft.Office.Interop.Excel.Application();
workbooks = application.Workbooks;
workbook = returnworkbook(FieldName, workbooks);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; //选择第一个表
Range range = worksheet.Cells[1, 8] as Range; //这是选择第一行第八列的内容
Range rangee = worksheet.Cells[1, 9] as Range; //这是第一行到九列的内容 string str1=range.Value2.ToString(); string str2=ragee.Value2.ToString(); ................... //你所要做的操作 .................. workbook.Close(Type.Missing, FieldName, Type.Missing);
workbooks.Close(); //退出关闭资源
application.Quit();
}
} private Workbook returnworkbook(string filename,Workbooks works) //这里是打一开一个工作表
{
Microsoft.Office.Interop.Excel.Workbook wk=works.Open(
filename, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return wk;
}

1.右击解决方案e799bee5baa6e4b893e5b19e333的引用,添加.NET中Microsoft.Office.Interop.Excel的引用;
2.在代码头添加using Microsoft.Office.Interop.Excel;
3.在函数中添加如下代码:
//创建
Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook1 = excel1.Workbooks.Add(true);
Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];

//写入
worksheet1.Cells[1, 1] = "航班号";
worksheet1.Cells[1, 2] = "起飞地点";
worksheet1.Cells[1, 3] = "降落地点";

//显示
excel1.Visible = true;

Console.ReadLine();

//保存文件
workbook1.Close(true, "d:\\1.xls", null);

1.先去导入7a64e58685e5aeb9333dll库:Microsoft.Office.Interop.Excel.dl

2.然后再去写代码:

using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using Microsoft.Office.Interop;  

using Microsoft.Office.Interop.Excel;  

using System.Windows.Forms;  

using Excel = Microsoft.Office.Interop.Excel;   

  

public class ExprotToExcel  

{   

    public void  DataToExcel(DataGridView dgv,ToolStripProgressBar tempProgressBar,ToolStripStatusLabel toolstrip)  

    {  

        if (dgv.Rows.Count == 0)  

        {  

            MessageBox.Show("无数据"); return;  

        }  

        MessageBox.Show("开始生成要导出的数据", "导出提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  

        Excel.Application excel = new Excel.Application();  

        excel.Application.Workbooks.Add(true);  

        excel.Visible = false;     

        for (int i = 0; i < dgv.ColumnCount; i++)  

            excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;  

        tempProgressBar.Visible = true;  

        tempProgressBar.Minimum = 1;  

        tempProgressBar.Maximum = dgv.RowCount;  

        tempProgressBar.Step = 1;  

        toolstrip.Visible = true;  

        for (int i = 0; i < dgv.RowCount; i++)  

        {  

            for (int j = 0; j < dgv.ColumnCount; j++)  

            {  

                if (dgv[j, i].ValueType == typeof(string))  

                {  

                    excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();  

                }  

                else  

                {  

                    excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();  

                }  

            }  

            toolstrip.Text = "|| 状态:正在生成第 "+i+"/"+dgv.RowCount+" 个";  

            tempProgressBar.Value = i + 1;  

        }  

        toolstrip.Text = "|| 状态:生成成功!";  

        MessageBox.Show("生成成功,请保存。","生成提示",MessageBoxButtons.OK,MessageBoxIcon.Information);  

        excel.Visible = true;  

    }  

}  

3.打开后,效果如下:



楼上导出的只是普通文本格式不是真正的excel文件,导出excel文件需要引用excel组件,在你的项目bin目录中右键选择引用找到类似Interop.Microsoft.Office.Interop.Excel.dll的文件引入进去,然后调用相关函数就可e79fa5e98193e4b893e5b19e335以了以下代码是我从我的项目中扣出来的,请自己改一下相关数据。usingMicrosoft.Office.Interop.Excel;GC.Collect();Applicationexcel;_WorkbookxBk;_WorksheetxSt;excel=newApplicationClass();xBk=excel.Workbooks.Add(true);xSt=(_Worksheet)xBk.ActiveSheet;////设置标题////设置标题excel.Cells[1,1]="姓名";excel.Cells[1,2]="身份证";xSt.get_Range(excel.Cells[1,1],excel.Cells[1,colCountStaff]).HorizontalAlignment=XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐for(inti=1;i<=rowCountStaff;i++){excel.Cells[i+1,1]="'"+listStaff[i-1].Name;excel.Cells[i+1,2]="'"+listStaff[i-1].CardNO;}for(inti=1;i<=rowCountCompany;i++){excel.Cells[i+1,5]="'"+listCompany[i-1].ID;excel.Cells[i+1,6]="'"+listCompany[i-1].Name;excel.Cells[i+1,7]="'"+listCompany[i-1].ParentID;}////显示效果////设置选中的部分的颜色xSt.get_Range(xSt.Cells[1,1],xSt.Cells[1,colCountStaff]).Select();xSt.get_Range(xSt.Cells[1,1],xSt.Cells[1,colCountStaff]).Interior.ColorIndex=19;//设置为浅黄色,共计有56种xSt.get_Range(xSt.Cells[1,1],xSt.Cells[rowCountStaff+1,colCountStaff]).Columns.AutoFit();//绘制边框xSt.get_Range(xSt.Cells[1,1],xSt.Cells[rowCountStaff+1,colCountStaff]).Borders.LineStyle=1;excel.Visible=false;stringfileName=DateTime.Now.ToString("yyyyMMddhhmmss");//设置导出文件的名称xBk.SaveCopyAs(Server.MapPath("~")+"\\Excel\\download\\"+fileName+".xls");//ds=null;xBk.Close(false,null,null);excel.Quit();System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);xBk=null;excel=null;xSt=null;GC.Collect();stringpath=Server.MapPath("~")+"\\Excel\\download\\"+fileName+".xls";System.IO.FileInfofile=newSystem.IO.FileInfo(path);Response.Clear();Response.Charset="GB2312";Response.ContentEncoding=System.Text.Encoding.UTF8;//添加头信息,为"文件下载/另存为"对话框指定默认文件名Response.AddHeader("Content-Disposition","attachment;filename="+Server.UrlEncode(file.Name));//添加头信息,指定文件大小,让浏览器能够显示下载进度Response.AddHeader("Content-Length",file.Length.ToString());//指定返回的是一个不能被客户端读取的流,必须被下载Response.ContentType="application/ms-excel";//把文件流发送到客户端Response.WriteFile(file.FullName);//停止页面的执行Response.End();

相关阅读

关键词不能为空
极力推荐
  • 工资用<em>EXCEL</em>公式怎么算?

  • =VLOOKUP(岗位,岗位津贴表!岗位列:津贴列,岗位列到津贴列之间的列数,0)excel中,如何实现根据岗位和级别来自动生成相应的工资" src="/uploads/tu/166.jpg" style="width: 400px; height: 267px;" /

ppt怎么做_excel表格制作_office365_word文档_365办公网