using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Data.SqlClient;
using Microsoft.Office.Core;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Office.Interop.Excel; //添加引用-->COM-->Microsoft Excel 12.0 Object Library namespace TestExcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static string connStr = "User Id=sa;Password=123456;Data Source=.;Initial Catalog=TDRDB;"; //查询产品
public static List<Object> getProducList()
{
SqlConnection conn = null;
List<Object> producList = new List<Object>();
string sqlStr = string.Format("select * from production order by type"); try
{
using (conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Production p = new Production();
p.Id = int.Parse(Convert.ToString(dr["ID"]));
p.Name = Convert.ToString(dr["name"]);
p.Type = Convert.ToString(dr["type"]);
p.Pic = Convert.ToString(dr["pic"]);
p.NetPrice = Double.Parse(Convert.ToString(dr["netPrice"]));
p.GrossPrice = Double.Parse(Convert.ToString(dr["grossPrice"]));
p.Mark = Convert.ToString(dr["mark"]);
producList.Add(p);
} }
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
conn.Dispose();
}
return producList; }
private void button1_Click(object sender, EventArgs e)
{ DataSet ds = new DataSet();
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("编号"), new DataColumn("名称"), new DataColumn("型号"), new DataColumn("图片"), new DataColumn("净价"), new DataColumn("毛价"), new DataColumn("备注"), });
for (int i = 0; i < getProducList().Count; i++)
{
Production produc=(Production)getProducList()[i];
String[] rows = new String[] { produc.Id.ToString(), produc.Name, produc.Type, produc.Pic, produc.NetPrice.ToString(), produc.GrossPrice.ToString(), produc.Mark };
dt.Rows.Add(rows);
}
ds.Tables.Add(dt);
ExportToExcel(ds, "D:\\产品信息"); } public static void ExportToExcel(DataSet dataSet, string fileName)
{ //Excel程序
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
excelApplication.DisplayAlerts = false;
//工作簿
Workbook workbook = excelApplication.Workbooks.Add(Missing.Value);
//上一个工作簿
Worksheet lastWorksheet = (Worksheet)workbook.Worksheets.get_Item(workbook.Worksheets.Count);
//空白工作簿
Worksheet newSheet = (Worksheet)workbook.Worksheets.Add(Type.Missing, lastWorksheet, Type.Missing, Type.Missing); foreach (System.Data.DataTable dt in dataSet.Tables)
{
newSheet.Name = dt.TableName; for (int col = 0; col < dt.Columns.Count; col++)
{
newSheet.Cells[1, col + 1] = dt.Columns[col].ColumnName;
}
for (int row = 0; row < dt.Rows.Count; row++)
{
for (int col = 0; col < dt.Columns.Count; col++)
{
newSheet.Cells[row + 2, col + 1] = dt.Rows[row][col].ToString();
}
}
} try
{
newSheet.Cells.Font.Size = 12;
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
//((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Activate();
workbook.Close(true, fileName, System.Reflection.Missing.Value);
MessageBox.Show("成功导出Excel,请查看!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
catch (Exception e)
{
throw e;
}
finally
{
excelApplication.Quit();
Process[] excelProcesses = Process.GetProcessesByName("EXCEL");
DateTime startTime = new DateTime(); int processId = 0;
for (int i = 0; i < excelProcesses.Length; i++)
{
if (startTime < excelProcesses[i].StartTime)
{
startTime = excelProcesses[i].StartTime;
processId = i;
}
} if (excelProcesses[processId].HasExited == false)
{
excelProcesses[processId].Kill();
}
} } }
}

最新文章

  1. IntelliJ IDEA WEB项目的部署配置
  2. jQuery判断元素是否存在方法
  3. 媒体查询判断ipad与iPhone各版本i
  4. ubuntu安装bower失败的解决方法
  5. What Controls are new for windows phone 8.1
  6. Codeforces Round #338 (Div. 2) E. Hexagons 讨论讨论
  7. Hadoop基于Protocol Buffer的RPC实现代码分析-Server端
  8. can&#39;t find -lsocket的解决办法
  9. codeblocks创建和使用静态库(C语言)
  10. 《A First Course in Probability》-chaper6-随机变量的联合分布-独立性
  11. 开始我的 JNI 入门吧
  12. ueditor编辑器使用总结
  13. URL末尾处的斜杠“/”
  14. Equinox OSGi应用嵌入Jersey框架搭建REST服务
  15. flask学习笔记(1)-虚拟环境安装
  16. AI matplotlib
  17. Sway
  18. QT listwiget 控件添加图片
  19. [CNN] Understanding Convolution
  20. 新书预告 ArcGIS跨平台开发系列第一本

热门文章

  1. 利用node、express初始化项目
  2. boost asio异步读写网络聊天程序客户端 实例详解
  3. PAT_A1125#Chain the Ropes
  4. mint-ui 取值
  5. laravel中ubuntu下执行php artisan migrate总是报错
  6. Linux目录与相关配置文件讲解
  7. 63.es中的type数据类型
  8. Linux拖拽上传文件 lrzsz
  9. win7的目录和vbox的共享,linux中没有权限打开
  10. 洛谷 P1198 BZOJ 1012 [JSOI2008]最大数