private void button2_Click(object sender, EventArgs e)
{
Productdiscount pd = new Productdiscount();
Serviceinformation si = new Serviceinformation();
if (MessageBox.Show("您确定要导出吗?", "提示",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
ExportDate ep = new ExportDate();
ep.dgvOutPutExcel(dataGridView1);
ep.dgvOutPutExcel(pd.dataGridView1);
ep.dgvOutPutExcel(si.dataGridView1); }
ep.dgvOutPutExcel(pd.dataGridView1);和ep.dgvOutPutExcel(si.dataGridView1);
取出来怎么合并啊,怎么显示啊,求代码。简单点就可以了
{
Productdiscount pd = new Productdiscount();
Serviceinformation si = new Serviceinformation();
if (MessageBox.Show("您确定要导出吗?", "提示",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
ExportDate ep = new ExportDate();
ep.dgvOutPutExcel(dataGridView1);
ep.dgvOutPutExcel(pd.dataGridView1);
ep.dgvOutPutExcel(si.dataGridView1); }
ep.dgvOutPutExcel(pd.dataGridView1);和ep.dgvOutPutExcel(si.dataGridView1);
取出来怎么合并啊,怎么显示啊,求代码。简单点就可以了
解决方案 »
- 自建SMTP邮件服务器如何统计发送数据?
- 欢迎大家到本人WPF博客留言讨论
- 这个正则表达式应该怎么写?
- 使用ServiceController.Pause() 无法暂停服务的问题
- 在WEB中如何在DataGrid绑定列中双击触发服务器事件? 如何在web的datagrid中鼠标放到一个单元格上鼠标成手状?
- 请问一个关于容器的问题.
- c#应用程序中如何隐藏MediaPlayer控件中的控制按钮
- winfrom窗口关闭问题
- C#和showModalDialog的问题,在先等待,立刻给分!
- 为什么只能给20分呢?
- 有没有能实现这种的: 1 2 3 4 8 7 6 5 9 10 11 12 .....
- 有个APS.NET的连接不知道错在哪里?
{
class ExportDate
{
public string lujing = System.Configuration.ConfigurationManager.AppSettings["lujing"];
public string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectionstring"];
public static IntPtr hwnd;
public static int ID = 0;
[DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public ExportDate() {
}
public void outPutExcel(System.Data.DataSet ds)
{
System.Data.DataTableCollection dtc = ds.Tables;
if (dtc[0].Rows.Count == 0)
{
MessageBox.Show("没数据");
}
else
{ Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
//clsLog.m_CreateErrorLog("无法创建Excel对象,可能计算机未安装Excel", "", "");
return;
}
//創建Excel對象
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
//Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
for (int sheetcount = 0; sheetcount < 1; sheetcount++)//循环根据自己需要的sheet的数目这里是两个
{
if (worksheet == null)
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
}
else
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, worksheet, 1, Type.Missing);
}
if (sheetcount == 0)
{
for (int j = 1; j <= dtc[0].Columns.Count; j++)
{
worksheet.Cells[1, j] = dtc[0].Columns[j-1].ColumnName;
}
for (int j = 2;j<dtc[0].Rows.Count;j++)
{
for (int y = 1; y <= dtc[0].Columns.Count; y++)
{
worksheet.Cells[j, y] = dtc[0].Rows[j][y-1].ToString();
}
}
}
}
//下面是将Excel存储在服务器上指定的路径与存储的名称
try
{
string tPath = lujing;
if (!Directory.Exists(tPath))
{
Directory.CreateDirectory(tPath);
}
string d = DateTime.Now.ToString("hhmm");
workbook.SaveAs(tPath + "\\" + d + ".xls");
workbooks.Open(tPath + "\\" + d + ".xls");
MessageBox.Show("导出成功!");
//EXEC master..xp_cmdshell 'bcp JapanseWriteProjectDataSet1.dbo.fileInformation out "d:\text.xls" /c -/S"20120424-1132" /U"sa" -P"111111"';
// EXEC master..xp_cmdshell 'bcp "SELECT imageName,loginData,policDepartment,MarkNumber,loginType,postalCode,homeAddress,streetAddress,apartment,name,nameHiragana,phone,carNumber,manufacturer,colorNumber,carType from fileInformation order by flieInfonmation_id desc" queryout "d:\text.xls" /c -/S"20120424-1132" /U"sa" -P"111111"';
}
catch (Exception ex)
{
string strMsgErr = "生成Excel附件過程中出現異常,詳細信息如:" + ex.ToString();
MessageBox.Show(strMsgErr); }
finally
{
xlApp.Quit();
//我们导出Excel的时候会在进程中调用Excel进程,导出之后必须强制杀掉进程
try
{
if (xlApp != null)
{ hwnd = new IntPtr(xlApp.Hwnd);
GetWindowThreadProcessId(hwnd, out ID);
System.Diagnostics.Process.GetProcessById(ID).Kill();
}
}
catch (Exception ex)
{
Console.WriteLine("Delete Excel Process Error:" + ex.Message);
}
}
} } public void dgvOutPutExcel(DataGridView dgv)
{
if (dgv.Rows.Count==0)
{
MessageBox.Show("没数据");
}
else
{ Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null)
{
//clsLog.m_CreateErrorLog("无法创建Excel对象,可能计算机未安装Excel", "", "");
return;
}
//創建Excel對象
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
//Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
for (int sheetcount = 0; sheetcount < 1; sheetcount++)//循环根据自己需要的sheet的数目这里是两个
{
if (worksheet == null)
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
}
else
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, worksheet, 1, Type.Missing);
}
if (sheetcount == 0)
{
int c = 1;
for (int j = 1; j <= dgv.Columns.Count; j++)
{ if (dgv.Columns[j - 1].Visible)
{
worksheet.Cells[1, c] = dgv.Columns[j - 1].HeaderText;
c++;
} } for (int j = 2; j < dgv.Rows.Count; j++)
{
int c2 = 1;
for (int y = 1; y <= dgv.Columns.Count; y++)
{
if (dgv.Rows[j].Cells[y - 1].Visible)
{
worksheet.Cells[j, c2] = dgv.Rows[j].Cells[y - 1].FormattedValue.ToString();
c2++;
} }
}
}
}
//下面是将Excel存储在服务器上指定的路径与存储的名称
try
{
string tPath = lujing;
if (!Directory.Exists(tPath))
{
Directory.CreateDirectory(tPath);
}
string d = DateTime.Now.ToString("hhmm");
workbook.SaveAs(tPath + "\\" + d + ".xls");
workbooks.Open(tPath + "\\" + d + ".xls");
MessageBox.Show("导出成功!");
EXEC master..xp_cmdshell 'bcp "SELECT imageName,loginData,policDepartment,MarkNumber,loginType,postalCode,homeAddress,streetAddress,apartment,name,nameHiragana,phone,carNumber,manufacturer,colorNumber,carType from fileInformation order by flieInfonmation_id desc" queryout "d:\text.xls" /c -/S"20120424-1132" /U"sa" -P"111111"';
}
//writeLog("生成Exce附件成功!");//此处是我的一个方法,记录log讯息的 catch (Exception ex)
{
string strMsgErr = "生成Excel附件過程中出現異常,詳細信息如:" + ex.ToString();
MessageBox.Show(strMsgErr); }
finally
{
xlApp.Quit();
//我们导出Excel的时候会在进程中调用Excel进程,导出之后必须强制杀掉进程
try
{
if (xlApp != null)
{ hwnd = new IntPtr(xlApp.Hwnd);
GetWindowThreadProcessId(hwnd, out ID);
System.Diagnostics.Process.GetProcessById(ID).Kill();
}
}
catch (Exception ex)
{
Console.WriteLine("Delete Excel Process Error:" + ex.Message);
}
}
} }
}
}