乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 在C#中,如何把<em>DataGrid</em>view中的数据导出到一个E...-datag

在C#中,如何把<em>DataGrid</em>view中的数据导出到一个E...-datag

作者:乔山办公网日期:

返回目录:excel表格制作


排序就是指排当前页,因为看不copy见的排序也没用。楼主要是想全局排序的话,可以使用服务器端排序,但那样的话会刷新整个表格的数据的。

easyui目前还没提供导出Excel的功能,tiger ui这个有导出Excel的功能。要导出Excel,可以使用PHPExcel这个风骚的类库,各种功能。

真是巧,我昨天刚刚做了这个
public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
{
if (gridView.Rows.Count == 0)
{
return false;
}
//创建Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);

//生成字段名称7a64e78988e69d83338
for (int i = 0; i < gridView.ColumnCount;i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < gridView.RowCount - 1; i++) //循环行
{
for(int j = 0;j < gridView.ColumnCount ;j++) //循环列
{
if(gridView[j,i].ValueType==typeof(string))
{
excel.Cells[i+2,j+1] = "'" + gridView.Rows[i].Cells[j].Value.ToString();
}
else
{
excel.Cells[i+2,j+1] = gridView.Rows[i].Cells[j].Value.ToString();
}
}
}
//设置禁止弹出保存和覆盖的询问提示框
excel.Visible = false;
excel.DisplayAlerts = false;
excel.AlertBeforeOverwriting = false;

//保存到临时工作簿
//excel.Application.Workbooks.Add(true).Save();
//保存文件

excel.Save("D:" + "\\234.xls");
excel.Quit();
return true;
}
2、做的系统是:机房收费系统
3、系统开发人员:黄爱岗
4、DataGridView获得数据:DataGridView1.DataSource=DataSet1.Tables(0)(注:通过查询SQL数据库将查询到的数据全部放入DataSet中,然后赋给DataGridView)
5、导出数据到Excel表:首先需要添加引用【项目(project)-添加引用(add reference)-Microsoft .Office.Interop.Excel】,其他代码如下:
Dim MyExcel As New Microsoft.Office.Interop.Excel.Application() '定义e69da5e887aae799bee5baa6e79fa5e98193336并实例化Excel工作表
MyExcel.Application.Workbooks.Add(True) '打开Excel工作簿
MyExcel.Visible = True 'Excel设置为可见的
Dim Col As Integer '定义整型变量ColTryFor Col = 0 To DataGridView1.ColumnCount – 1 'Col的变化范围
MyExcel.Cells(1, Col + 1) = Me.DataGridView1.Columns(Col).HeaderText '添加列标题Next ColCatch ex As Exception
MsgBox(ex.Message) '弹出捕获的消息
Exit Sub '退出程序End Try
Dim i As Integer '定义整型行变量i
Dim j As Integer '定义整型列变量jTryFor i = 0 To DataGridView1.RowCount – 2 '行变量i的取值范围
For j = 0 To DataGridView1.ColumnCount – 1 '列变量j的取值范围
If Me.DataGridView1(j, i).Value IsNot System.DBNull.Value Then
MyExcel.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString() '将DataGridview中的数据添加到Excel表中End IfNext jNext iMsgBox(导出成功!) '提示导出成功
Catch ex As Exception
MsgBox(ex.Message) '弹出捕获的消息

因为excel数据写入涉及到字体,样式等多个方面,所以相对来说要复杂一些,可以参考下以下导出为excel方法代码:
import jxl.*;
import jxl.write.*;
import java.io.*;
import java.io.File.*;
import java.util.*;

public class excel
{
public static void main(String[] args)
{

String targetfile = "c:/out.xls";//输出的excel文件名
String worksheet = "List";//输出的excel文件工作表名
String[] title = {"ID","NAME","DESCRIB"};//excel工作表的标题

WritableWorkbook workbook;
try
{
//创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下
//workbook = Workbook.createWorkbook(new File("output.xls"));
System.out.println("begin");

OutputStream os=new FileOutputStream(targetfile);
workbook=Workbook.createWorkbook(os);

WritableSheet sheet = workbook.createSheet(worksheet, 0); //添加第一个工作表
//WritableSheet sheet1 = workbook.createSheet("MySheet1", 1); //可添加第二个7a64e58685e5aeb9331工作
/*
jxl.write.Label label = new jxl.write.Label(0, 2, "A label record"); //put a label in cell A3, Label(column,row)
sheet.addCell(label);
*/

jxl.write.Label label;
for (int i=0; i<title.length; i++)
{
//Label(列号,行号 ,内容 )
label = new jxl.write.Label(i, 0, title[i]); //put the title in row1
sheet.addCell(label);
}

//下列添加的对字体等的设置均调试通过,可作参考用

//添加数字
jxl.write.Number number = new jxl.write.Number(3, 4, 3.14159); //put the number 3.14159 in cell D5
sheet.addCell(number);

//添加带有字型Formatting的对象
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES,10,WritableFont.BOLD,true);
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
jxl.write.Label labelCF = new jxl.write.Label(4,4,"文本",wcfF);
sheet.addCell(labelCF);

//添加带有字体颜色,带背景颜色 Formatting的对象
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
wcfFC.setBackground(jxl.format.Colour.BLUE);
jxl.write.Label labelCFC = new jxl.write.Label(1,5,"带颜色",wcfFC);
sheet.addCell(labelCFC);

//添加带有formatting的Number对象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
jxl.write.Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
sheet.addCell(labelNF);

//3.添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0,2,false);
sheet.addCell(labelB);

//4.添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
sheet.addCell(labelDT);

//添加带有formatting的DateFormat对象
jxl.write.DateFormat df = new jxl.write.DateFormat("ddMMyyyyhh:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1,3,new java.util.Date(),wcfDF);
sheet.addCell(labelDTF);

//和宾单元格
//sheet.mergeCells(int col1,int row1,int col2,int row2);//左上角到右下角
sheet.mergeCells(4,5,8,10);//左上角到右下角
wfc = new jxl.write.WritableFont(WritableFont.ARIAL,40,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
jxl.write.WritableCellFormat wchB = new jxl.write.WritableCellFormat(wfc);
wchB.setAlignment(jxl.format.Alignment.CENTRE);
labelCFC = new jxl.write.Label(4,5,"单元合并",wchB);
sheet.addCell(labelCFC); //

//设置边框
jxl.write.WritableCellFormat wcsB = new jxl.write.WritableCellFormat();
wcsB.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THICK);
labelCFC = new jxl.write.Label(0,6,"边框设置",wcsB);
sheet.addCell(labelCFC);
workbook.write();
workbook.close();
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println("end");
Runtime r=Runtime.getRuntime();
Process p=null;
//String cmd[]={"notepad","exec.java"};
String cmd[]={"C:\\Program Files\\Microsoft Office\\Office\\EXCEL.EXE","out.xls"};
try{
p=r.exec(cmd);
}
catch(Exception e){
System.out.println("error executing: "+cmd[0]);
}

}
}

相关阅读

关键词不能为空
极力推荐

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