using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using DBUtility;
using System.IO;
using System.Runtime.InteropServices;
namespace SCM.公用类
{
class Xt_BaseBillFun
{ //初始化网格
public static void initGridFst(DataGridView grdMain, string Name)
{
grdMain.RowTemplate.Height = 18; //行高设置
grdMain.RowTemplate.MinimumHeight = 18; //最小行高
grdMain.ColumnHeadersHeight = 40; //标题行高
grdMain.RowHeadersVisible = false; //固定列是否显示
grdMain.ColumnHeadersVisible = true; //固定行是否显示
grdMain.RowCount = 1; //总行数
for (int i = 0; i < grdMain.ColumnCount; i++)
{
grdMain.Columns[i].Width = 100; //设置 列宽
grdMain.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; //列 排序模式
}
//
grdMain.RowsDefaultCellStyle.SelectionForeColor = Color.Black; //行选中字体颜色
//grdMain.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; //内容布局(居中,靠右)
grdMain.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//标题行布局(居中,靠右
grdMain.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True;//标题行自动换行
grdMain.SelectionMode = DataGridViewSelectionMode.CellSelect; //选行模式
grdMain.ReadOnly = false; //是否只读
grdMain.EditMode = DataGridViewEditMode.EditOnEnter; //编辑模式
grdMain.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
grdMain.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; //标题行高 调整模式
grdMain.EnableHeadersVisualStyles = false; //标题 显示样式
grdMain.AllowUserToAddRows = false; //是否允许自增--否
grdMain.AllowUserToDeleteRows = false; //是否允许删除--否
grdMain.AllowUserToResizeColumns = true; //允许调整列宽--是
grdMain.AllowUserToResizeRows = false; //允许调整行高--否
grdMain.AllowUserToOrderColumns = false; //是否允许拖拽列
grdMain.BackgroundColor = Color.White; //网格背景色
grdMain.GridColor = Color.LightGray; //网格线颜色
grdMain.DefaultCellStyle.SelectionBackColor = Color.AntiqueWhite; //选中时背景颜色
grdMain.ShowCellToolTips = false; //鼠标停留 显示提示
grdMain.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSteelBlue; //标题列 背景色
ClsPub1.GetGridView(grdMain, Name, DBUtility.ClsPub.AppPath);//设置列宽
ClsPub1.HideGridView(grdMain, Name, DBUtility.ClsPub.AppPath);//设置列宽
grdMain.RowCount = 100; //总行数(更改行数时,会触发 编辑前事件)
}
//显示列表
///
/// 设置列宽,隐藏列(h开头、用户设置),冻结列,对齐方式
///
///
///
///
///
public static void DisplayGrid(DataGridView grdMain) //
{
//加载列宽,隐藏列(H开头)
for (int i = 0; i < grdMain.ColumnCount; i++)
{
//grdMain.Columns[i].Width = 100;//默认列宽
if (grdMain.Columns[i].HeaderText.Substring(0, 1).ToLower() == "h")//隐藏H开头的列
{
grdMain.Columns[i].Visible = false;
}
else
{
grdMain.Columns[i].Visible = true;
}
}
}
///
/// 网格 导出EXCEL 网格,文件名,标题名
///
public static void DataGridViewToExcel2(DataGridView grdMain, string sText, string sBTText)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl文件(*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "数据视图导出EXCEL文件";
saveFileDialog.FileName = sText;
//saveFileDialog.ShowDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
string str = "";
DateTime start = DateTime.Now;
try
{
if (sBTText != "")
{
sw.WriteLine(sBTText);
}
//写标题
bool sFirstCol = true;
for (int i = 0; i < grdMain.ColumnCount; i++)
{
if (i > 0 && grdMain.Columns[i].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[i].HeaderText.Substring(0, 1) != "H" && sFirstCol != true)
{
str += "\t";
}
if (grdMain.Columns[i].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[i].HeaderText.Substring(0, 1) != "H")
{
sFirstCol = false;
str += grdMain.Columns[i].HeaderText;
}
}
str = str.Replace("\n", "");
sw.WriteLine(str);
//写内容
for (int j = 0; j < grdMain.Rows.Count; j++)
{
string tempStr = "";
sFirstCol = true;
for (int k = 0; k < grdMain.Columns.Count; k++)
{
if (k > 0 && grdMain.Columns[k].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[k].HeaderText.Substring(0, 1) != "H" && sFirstCol != true)
{
tempStr += "\t";
}
if (grdMain.Columns[k].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[k].HeaderText.Substring(0, 1) != "H")
{
sFirstCol = false;
if (grdMain.Rows[j].Cells[k].Value == null)
{
tempStr += string.Empty;
}
else
{
tempStr += grdMain.Rows[j].Cells[k].Value.ToString();
}
}
}
tempStr = tempStr.Replace("\n", "");
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sw.Close();
myStream.Close();
}
MessageBox.Show("将此工作表导出为excel共耗时:" + DateTime.Now.Subtract(start).TotalMilliseconds.ToString() + "毫秒");
}
}
//数据导出 导出CSV格式
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public static void DataToExcel(string sText, DataGridView grdMain)
{
string saveFileName = "";
bool fileSaved = false;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = sText;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
//
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel", "提示");
return;
}
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
//写入 标题
int j = 0;
for (int i = 0; i < grdMain.ColumnCount; i++)
{
if (grdMain.Columns[i].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[i].HeaderText.Substring(0, 1) != "H")
{
worksheet.Cells[1, i + 1 - j] = ClsPub.isStrNull(grdMain.Columns[i].HeaderText);
}
else
{
j = j + 1;
}
//worksheet.Cells[1, i + 1] = ClsPub.isStrNull(grdMain.Columns[i].HeaderText);
}
//写入内容
for (int r = 0; r < grdMain.RowCount; r++)
{
int a = 0;
for (int i = 0; i < grdMain.ColumnCount; i++)
{
if (grdMain.Columns[i].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[i].HeaderText.Substring(0, 1) != "H")
{
worksheet.Cells[r + 2, i + 1 - a] = "'" + ClsPub.isStrNull(grdMain.Rows[r].Cells[i].Value);
}
else
{
a = a + 1;
}
//worksheet.Cells[r + 2, i + 1] = "'" + ClsPub.isStrNull(grdMain.Rows[r].Cells[i].Value);
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message, "提示");
}
}
else
{
fileSaved = false;
}
xlApp.Quit();
//
IntPtr t = new IntPtr(xlApp.Hwnd); //得到这个句柄,具体作用是得到这块内存入口
int k = 0;
GetWindowThreadProcessId(t, out k); //得到本进程唯一标志k
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //得到对进程k的引用
p.Kill(); //关闭进程k
xlApp = null;
//GC.Collect();//强行销毁
//if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL
MessageBox.Show("引出完毕!");
}
}
}