21.
① 怎样把 DataGrid 的数据导出到 Excel 以供打印?
② 之前已经为 DataGrid 设置了 TableStyle ,即自定义了列标题和要显示的列,如果想以自定义的视图导出数据该怎么办?
③ 把数据导出到 Excel 后,怎样为它设置边框啊?
④ 怎样使从 DataGrid 导出到 Excel 的某个列居中对齐?
⑤ 数据从 DataGrid 导出到 Excel 后,怎样使标题行在打印时出现在每一页?
⑥ DataGrid 数据导出到 Excel 后打印时每一页显示 ’ 当前页 / 共几页 ’ ,怎样实现?
答 :
① private void button1_Click(object sender, System.EventArgs e)
{
int row_index, col_i ndex;
row_index = 1;
col_index = 1;
Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Workbooks.Add(true);
DataTable dt = ds.Tables["table"];
foreach(DataColumn dcHeader in dt.Columns)
excel.Cells[row_index, col_index++] = dcHeader.ColumnName;
foreach(DataRow dr in dt.Rows)
{
col_index = 0;
foreach(DataColumn dc in dt.Columns)
{ excel.Cells[row_index+1, col_index+1] = dr[dc];
col_index++;
}
row_index++; }
excel.Visible = true; }
private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("server=tao;uid=sa;pwd=;database=pubs" ;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from authors", conn);
ds = new DataSet();
da.Fill(ds, "table" ;
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "table";
}
② data Grid1.TableStyles[0].GridColumnStyles[index].HeaderText;//index 可以从 0~dataGrid1.TableStyles[0].GridColumnStyles.Count 遍历。
③ Excel.Range range;
range=worksheet.get_Range(worksheet.Cells[1,1],xSt.Cells[ds.Tables[0].Rows.Count+1,ds.Tables[0].Columns.Coun t]);
range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
④ range.Horizontal Alignment = Excel.XlHAlign.xlHAlignCenter
⑤ worksheet.PageSetup.PrintTitleRows = " $ 1: $ 1";
⑥ worksheet.PageSetup.CenterFooter = " 第 &P 页 / 共 &N 页 ";
22. 当把 DataGrid 的 Cell 内容赋值到 Excel 的过程中想在 DataGrid 的 CaptionText 上显示进度,但不显示。 WHY ?
... dataGrid1.CaptionText = " 正在导出: " + (row + 1) + "/" + row_cnt;
System.Windows.Forms.Application.DoEvents();
... 处理当前在消息队列中的所有 Windows 消息。
当运行 Windows 窗体时,它将创建新窗体,然后该窗体等待处理事件。该窗体在每次处理事件时,均将处理与该事件关联的所有代码。所有其他事件在队列中等待。在代码处理事件时,应用程序并不响应。如果在代码中调用 DoEvents, 则应用程序可以处理其他事件。
如果从代码中移除 DoEvents ,那么在按钮的单机事件处理程序执行结束以前,窗体不会重新绘制。通常在循环中使用该方法来处理消息。
23. 怎样从 Flash 调用外部程序,如一个 C# 编译后生成的 .exe? fscommand("exec", " 应用程序 .exe" ;
① 必须把 flash 发布为 .exe
② 必须在 flash 生成的 .exe 文件所在目录建一个名为 fscommand 的子目录,并把要调用的可执行程序拷贝到那里。
24. 有没有办法用代码控制 DataGrid 的上下、左右的滚动?
dataGrid1.Select();
SendKeys.Send("{PGUP}" ;
SendKeys.Send("{PGDN}" ;
SendKeys.Send("{^{LEFT}" ; // Ctrl+ 左方向键
SendKeys.Send("{^{RIGHT}" ; // Ctrl+ 右方向键
25. 怎样使两个 DataGrid 绑定两个主从关系的表?
DataGrid1.DataSource = ds;
DataGrid1.DataMember = " 母表 ";
...
DataGrid2.DataSouce = ds;
DataGrid2.DataMember = " 母表 . 关系名 ";
26.assembly 的版本号怎样才能自动生成?特别是在 Console 下没有通过 VStudio 环境编写程序时。
关键是 AssemblyInfo.cs 里的 [assembly: AssemblyVersion("1.0.*" ] ,命令行编译时包含 AssemblyInfo.cs
27. 怎样建立一个 Shared Assembly ?
用 sn.exe 生成一个 Strong Name : keyfile.sn ,放在源程序目录下
在项目的 AssemblyInfo.cs 里 [assembly: AssemblyKeyFile("..//..//keyfile.sn" ]
生成 dll 后,用 gacutil /i myDll.dll 放进 Global Assembly Cach.
28. 在 Oracle 里如何取得某字段第一个字母为大写英文 A~Z 之间的记录?
select * from table where ascii(substr( 字段 ,1,1)) between ascii('A') and ascii('Z') 29. 怎样取得当前 Assembly 的版本号?
Process current = Process.GetCurrentProcess();
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(current.MainModule.FileName);
Console.WriteLine(myFileVersionInfo.FileVersion);
30. 怎样制作一个简单的 winform 安装程序?
① 建一个 WinForm 应用程序,最最简单的那种。运行。
② 添加新项目 -> 安装和部署项目, ‘ 模板 ’ 选择 ‘ 安装向导 ’ 。
③ 连续二个 ‘ 下一步 ’ ,在 ‘ 选择包括的项目输出 ’ 步骤打勾 ‘ 主输出来自 ’ ,连续两个 ‘ 下一步 ’ , ‘ 完成 ’ 。
④ 生成。
⑤ 到项目目录下找到 Setup.exe (还有一个 .msi 和 .ini 文件),执行。
31. 怎样通过 winform 安装程序在 Sql Server 数据库上建表?
① [ 项目 ]—[ 添加新项 ] 类别:代码;模板:安装程序类。
名称: MyInstaller.cs
② 在 SQL Server 建立一个表,再 [ 所有任务 ]—[ 生成 SQL 脚本 ] 。
生成类似如下脚本(注意:把所有 GO 语句去掉):
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MyTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[MyTable]
CREATE TABLE [dbo].[MyTable] (
[ID] [int] NOT NULL ,
[NAME] [nchar] (4) COLLATE Chinese_PRC_CI_AS NOT NULL
ON [PRIMARY] ALTER TABLE [dbo].[MyTable] WITH NOCHECK ADD
CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED
( [ID] ON [PRIMARY]
③ [ 项目 ]—[ 添加现有项 ] 。 mytable.sql—[ 生成操作 ]-[ 嵌入的资源 ] 。
④ 将 MyInstaller.cs 切换到代码视图 , 添加下列代码:
先增加: using System.Reflection; using System.IO;
然后: private string GetSql(string Name)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name); StreamReader reader = new StreamReader(strm);
return reader.ReadToEnd();
}
catch (Exception ex)
{
Console.Write("In GetSql:"+ex.Message);
throw ex;
}
}
private void ExecuteSql(string DataBaseName,string Sql)
{
System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();
sqlConn.ConnectionString = "server=myserver;uid=sa;password=;database=master";
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConn);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName)
{
try
{
ExecuteSql("master","create DATABASE "+ strDBName);
ExecuteSql(strDBName,GetSql("mytable.sql" );
}
catch(Exception ex)
{
Console.Write("In exception handler :"+ex.Message);
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
AddDBTable("MyDB" ; // 建一个名为 MyDB 的 DataBase
}
⑤ [ 添加新项目 ]—[ 项目类型:安装和部署项目 ]—[ 模板:安装项目 ]—[ 名称: MySetup] 。
⑥ [ 应用程序文件夹 ]—[ 添加 ]—[ 项目输出 ]—[ 主输出 ] 。
⑦ 解决方案资源管理器 — 右键 —[ 安装项目 (MySetup)]—[ 视图 ]—[ 自定义操作 ] 。 [ 安装 ]—[ 添加自定义操作 ]—[ 双击:应用程序文件夹 ] 的 [ 主输出来自 ***( 活动 )] 。
① 怎样把 DataGrid 的数据导出到 Excel 以供打印?
② 之前已经为 DataGrid 设置了 TableStyle ,即自定义了列标题和要显示的列,如果想以自定义的视图导出数据该怎么办?
③ 把数据导出到 Excel 后,怎样为它设置边框啊?
④ 怎样使从 DataGrid 导出到 Excel 的某个列居中对齐?
⑤ 数据从 DataGrid 导出到 Excel 后,怎样使标题行在打印时出现在每一页?
⑥ DataGrid 数据导出到 Excel 后打印时每一页显示 ’ 当前页 / 共几页 ’ ,怎样实现?
答 :
① private void button1_Click(object sender, System.EventArgs e)
{
int row_index, col_i ndex;
row_index = 1;
col_index = 1;
Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Workbooks.Add(true);
DataTable dt = ds.Tables["table"];
foreach(DataColumn dcHeader in dt.Columns)
excel.Cells[row_index, col_index++] = dcHeader.ColumnName;
foreach(DataRow dr in dt.Rows)
{
col_index = 0;
foreach(DataColumn dc in dt.Columns)
{ excel.Cells[row_index+1, col_index+1] = dr[dc];
col_index++;
}
row_index++; }
excel.Visible = true; }
private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("server=tao;uid=sa;pwd=;database=pubs" ;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from authors", conn);
ds = new DataSet();
da.Fill(ds, "table" ;
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "table";
}
② data Grid1.TableStyles[0].GridColumnStyles[index].HeaderText;//index 可以从 0~dataGrid1.TableStyles[0].GridColumnStyles.Count 遍历。
③ Excel.Range range;
range=worksheet.get_Range(worksheet.Cells[1,1],xSt.Cells[ds.Tables[0].Rows.Count+1,ds.Tables[0].Columns.Coun t]);
range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
④ range.Horizontal Alignment = Excel.XlHAlign.xlHAlignCenter
⑤ worksheet.PageSetup.PrintTitleRows = " $ 1: $ 1";
⑥ worksheet.PageSetup.CenterFooter = " 第 &P 页 / 共 &N 页 ";
22. 当把 DataGrid 的 Cell 内容赋值到 Excel 的过程中想在 DataGrid 的 CaptionText 上显示进度,但不显示。 WHY ?
... dataGrid1.CaptionText = " 正在导出: " + (row + 1) + "/" + row_cnt;
System.Windows.Forms.Application.DoEvents();
... 处理当前在消息队列中的所有 Windows 消息。
当运行 Windows 窗体时,它将创建新窗体,然后该窗体等待处理事件。该窗体在每次处理事件时,均将处理与该事件关联的所有代码。所有其他事件在队列中等待。在代码处理事件时,应用程序并不响应。如果在代码中调用 DoEvents, 则应用程序可以处理其他事件。
如果从代码中移除 DoEvents ,那么在按钮的单机事件处理程序执行结束以前,窗体不会重新绘制。通常在循环中使用该方法来处理消息。
23. 怎样从 Flash 调用外部程序,如一个 C# 编译后生成的 .exe? fscommand("exec", " 应用程序 .exe" ;
① 必须把 flash 发布为 .exe
② 必须在 flash 生成的 .exe 文件所在目录建一个名为 fscommand 的子目录,并把要调用的可执行程序拷贝到那里。
24. 有没有办法用代码控制 DataGrid 的上下、左右的滚动?
dataGrid1.Select();
SendKeys.Send("{PGUP}" ;
SendKeys.Send("{PGDN}" ;
SendKeys.Send("{^{LEFT}" ; // Ctrl+ 左方向键
SendKeys.Send("{^{RIGHT}" ; // Ctrl+ 右方向键
25. 怎样使两个 DataGrid 绑定两个主从关系的表?
DataGrid1.DataSource = ds;
DataGrid1.DataMember = " 母表 ";
...
DataGrid2.DataSouce = ds;
DataGrid2.DataMember = " 母表 . 关系名 ";
26.assembly 的版本号怎样才能自动生成?特别是在 Console 下没有通过 VStudio 环境编写程序时。
关键是 AssemblyInfo.cs 里的 [assembly: AssemblyVersion("1.0.*" ] ,命令行编译时包含 AssemblyInfo.cs
27. 怎样建立一个 Shared Assembly ?
用 sn.exe 生成一个 Strong Name : keyfile.sn ,放在源程序目录下
在项目的 AssemblyInfo.cs 里 [assembly: AssemblyKeyFile("..//..//keyfile.sn" ]
生成 dll 后,用 gacutil /i myDll.dll 放进 Global Assembly Cach.
28. 在 Oracle 里如何取得某字段第一个字母为大写英文 A~Z 之间的记录?
select * from table where ascii(substr( 字段 ,1,1)) between ascii('A') and ascii('Z') 29. 怎样取得当前 Assembly 的版本号?
Process current = Process.GetCurrentProcess();
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(current.MainModule.FileName);
Console.WriteLine(myFileVersionInfo.FileVersion);
30. 怎样制作一个简单的 winform 安装程序?
① 建一个 WinForm 应用程序,最最简单的那种。运行。
② 添加新项目 -> 安装和部署项目, ‘ 模板 ’ 选择 ‘ 安装向导 ’ 。
③ 连续二个 ‘ 下一步 ’ ,在 ‘ 选择包括的项目输出 ’ 步骤打勾 ‘ 主输出来自 ’ ,连续两个 ‘ 下一步 ’ , ‘ 完成 ’ 。
④ 生成。
⑤ 到项目目录下找到 Setup.exe (还有一个 .msi 和 .ini 文件),执行。
31. 怎样通过 winform 安装程序在 Sql Server 数据库上建表?
① [ 项目 ]—[ 添加新项 ] 类别:代码;模板:安装程序类。
名称: MyInstaller.cs
② 在 SQL Server 建立一个表,再 [ 所有任务 ]—[ 生成 SQL 脚本 ] 。
生成类似如下脚本(注意:把所有 GO 语句去掉):
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MyTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[MyTable]
CREATE TABLE [dbo].[MyTable] (
[ID] [int] NOT NULL ,
[NAME] [nchar] (4) COLLATE Chinese_PRC_CI_AS NOT NULL
ON [PRIMARY] ALTER TABLE [dbo].[MyTable] WITH NOCHECK ADD
CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED
( [ID] ON [PRIMARY]
③ [ 项目 ]—[ 添加现有项 ] 。 mytable.sql—[ 生成操作 ]-[ 嵌入的资源 ] 。
④ 将 MyInstaller.cs 切换到代码视图 , 添加下列代码:
先增加: using System.Reflection; using System.IO;
然后: private string GetSql(string Name)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name); StreamReader reader = new StreamReader(strm);
return reader.ReadToEnd();
}
catch (Exception ex)
{
Console.Write("In GetSql:"+ex.Message);
throw ex;
}
}
private void ExecuteSql(string DataBaseName,string Sql)
{
System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();
sqlConn.ConnectionString = "server=myserver;uid=sa;password=;database=master";
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConn);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName)
{
try
{
ExecuteSql("master","create DATABASE "+ strDBName);
ExecuteSql(strDBName,GetSql("mytable.sql" );
}
catch(Exception ex)
{
Console.Write("In exception handler :"+ex.Message);
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
AddDBTable("MyDB" ; // 建一个名为 MyDB 的 DataBase
}
⑤ [ 添加新项目 ]—[ 项目类型:安装和部署项目 ]—[ 模板:安装项目 ]—[ 名称: MySetup] 。
⑥ [ 应用程序文件夹 ]—[ 添加 ]—[ 项目输出 ]—[ 主输出 ] 。
⑦ 解决方案资源管理器 — 右键 —[ 安装项目 (MySetup)]—[ 视图 ]—[ 自定义操作 ] 。 [ 安装 ]—[ 添加自定义操作 ]—[ 双击:应用程序文件夹 ] 的 [ 主输出来自 ***( 活动 )] 。

1万+

被折叠的 条评论
为什么被折叠?



