using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Pub_Class;
namespace WarM
{
public partial class Gy_DataIn_UpDateBarCodeStatus : Form
{
SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
public long HItemID;//ÏîÄ¿ID
private string fileName = null; //ÎļþÃû
private IWorkbook workbook = null;
private FileStream fs = null;
private bool disposed;
public Gy_DataIn_UpDateBarCodeStatus()
{
InitializeComponent();
}
private void initHeadCaption()
{
}
private void yl_Click(object sender, EventArgs e)
{
WriteClass();
}
//Êý¾Ýµ¼Èë
public void WriteClass()
{
int sHBarCodeCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÌõÂë±àºÅ", grdmain);
int sHBarCodeStatusCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÌõÂë״̬", grdmain);
if (sHBarCodeCol == -1)
{
MessageBox.Show("Ëùµ¼ÈëExcelÎĵµÖбØÐëÓС¾ÌõÂë±àºÅ¡¿ÁУ¡", "Ìáʾ");
return;
}
if (sHBarCodeStatusCol == -1)
{
MessageBox.Show("Ëùµ¼ÈëExcelÎĵµÖбØÐëÓС¾ÌõÂë״̬¡¿ÁУ¡", "Ìáʾ");
return;
}
//ÊÇ·ñ´æÔÚÐèÒªµ¼ÈëµÄÊý¾Ý
bool b = false;
string sHRemark = "";
string HMaker = ClsPub.CurUserName; //±ä¸üÈË
string HBarCode = ""; //ÌõÂë±àºÅ
string HBarCodeStatus = ""; //ÌõÂë״̬
for (int i = 0; i <= grdmain.Rows.Count - 1; i++)
{
HBarCode = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHBarCodeCol].Value);
HBarCodeStatus = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHBarCodeStatusCol].Value);
//±ØÌîÏîÅжÏ
if (HBarCode == "")
{
sHRemark = sHRemark + "µÚ" + DBUtility.ClsPub.isStrNull(i + 1) + "ÐУ¬ÌõÂë±àºÅ²»ÄÜΪ¿Õ£¡";
}
else
{
b = true;
}
if (HBarCodeStatus != "³ö¿â" && HBarCodeStatus != "Èë¿â" && HBarCodeStatus != "Òì³£" && HBarCodeStatus != "")
{
sHRemark = sHRemark + "µÚ" + DBUtility.ClsPub.isStrNull(i + 1) + "ÐУ¬ÌõÂë״ֵ̬²»ÕýÈ·£¬ÊäÈëÖµ±ØÐëΪÈë¿â¡¢³ö¿â¡¢Òì³£»ò¿ÕÖµ£¡";
}
}
if (b == false)
{
MessageBox.Show("ûÓÐÐèÒª±ä¸ü״̬µÄÊý¾Ý£¬ÇëÏÈÑ¡ÔñÐèÒª±ä¸ü״̬µÄÌõÂëËùÔÚEXCELÎļþ£¡", "Ìáʾ");
return;
}
if (sHRemark != "")
{
MessageBox.Show(sHRemark);
return;
}
for (int i = 0; i <= grdmain.Rows.Count - 1; i++)
{
HBarCode = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHBarCodeCol].Value);
HBarCodeStatus = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHBarCodeStatusCol].Value);
if (HBarCode != "")
{
//¸üÐÂÌõÂë״̬
oCn.RunProc("update Gy_BarCodeBill set HBarCodeStatus='" + HBarCodeStatus + "',HStatusMan='" + HMaker + "',HStatusDate=getdate() where HBarCode='" + HBarCode + "'", ref DBUtility.ClsPub.sExeReturnInfo);
}
}
MessageBox.Show("ÌõÂë״̬¸üÐÂÍê±Ï£¡");
}
//дÈëÀà ´Ó¿Ø¼þ
private bool Mater_WriteClass(int row)
{
return true;
}
private void tc_Click(object sender, EventArgs e)
{
this.Close();
}
private void OutMachFeeDataIn_Load(object sender, EventArgs e)
{
initHeadCaption();
}
public string ExcelPath;
///
/// ½«excelÖеÄÊý¾Ýµ¼Èëµ½DataTableÖÐ
///
/// excel·¾¶
/// excel¹¤×÷±¡sheetµÄÃû³Æ
/// µÚÒ»ÐÐÊÇ·ñÊÇDataTableµÄÁÐÃû
/// ·µ»ØµÄDataTable
public DataTable ExcelToDataTable(string fileName, string sheetName, bool isFirstRowColumn)
{
ISheet sheet = null;
DataTable data = new DataTable();
int startRow = 0;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
if (fileName.IndexOf(".xlsx") > 0) // 2007°æ±¾
workbook = new XSSFWorkbook(fs);
else if (fileName.IndexOf(".xls") > 0) // 2003°æ±¾
workbook = new HSSFWorkbook(fs);
if (sheetName != null)
{
sheet = workbook.GetSheet(sheetName);
if (sheet == null) //Èç¹ûûÓÐÕÒµ½Ö¸¶¨µÄsheetName¶ÔÓ¦µÄsheet£¬Ôò³¢ÊÔ»ñÈ¡µÚÒ»¸ösheet
{
sheet = workbook.GetSheetAt(0);
}
}
else
{
sheet = workbook.GetSheetAt(0);
}
if (sheet != null)
{
IRow firstRow = sheet.GetRow(0);
int cellCount = firstRow.LastCellNum; //Ò»ÐÐ×îºóÒ»¸öcellµÄ±àºÅ ¼´×ܵÄÁÐÊý
if (isFirstRowColumn)
{
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
ICell cell = firstRow.GetCell(i);
if (cell != null)
{
string cellValue = cell.StringCellValue;
if (cellValue != null)
{
DataColumn column = new DataColumn(cellValue);
data.Columns.Add(column);
}
}
}
startRow = sheet.FirstRowNum + 1;
}
else
{
startRow = sheet.FirstRowNum;
}
//×îºóÒ»ÁеıêºÅ
int rowCount = sheet.LastRowNum;
for (int i = startRow; i <= rowCount; ++i)
{
IRow row = sheet.GetRow(i);
if (row == null) continue; //ûÓÐÊý¾ÝµÄÐÐĬÈÏÊÇnull¡¡¡¡¡¡¡¡¡¡¡¡¡¡
DataRow dataRow = data.NewRow();
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
if (row.GetCell(j) != null) //ͬÀí£¬Ã»ÓÐÊý¾ÝµÄµ¥Ôª¸ñ¶¼Ä¬ÈÏÊÇnull
dataRow[j] = row.GetCell(j).ToString();
}
data.Rows.Add(dataRow);
}
}
return data;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
return null;
}
}
DataTable d;
private void button1_Click_1(object sender, EventArgs e)
{
if (txtHRemark.Text.Trim() == "")
{
txtHRemark.Text = "1";
//MessageBox.Show("ÇëÊäÈëÐèÒªµ¼ÈëµÄ¹¤×÷±¡Ãû³Æ£¡");
//return;
}
OpenExcelFile.Title = "Open Excel File ";
OpenExcelFile.FileName = "";
OpenExcelFile.Filter = "Microsoft Office Excel Files(*.xls)|*.xls";
OpenExcelFile.ShowDialog();
if (OpenExcelFile.FileName != "")
{
ExcelPath = OpenExcelFile.FileName;
d = ExcelToDataTable(ExcelPath, txtHRemark.Text.Trim(), true);
if (d == null)
{
MessageBox.Show("ËùÑ¡ÔñExeclÎĵµ´¦ÓÚ´ò¿ª×´Ì¬£¬Çë¹Ø±ÕºóÖØÐÂÑ¡Ôñµ¼È룡", "Ìáʾ");
return;
}
grdmain.DataSource = d.DefaultView;
}
else
{
ExcelPath = "";
}
}
private Int32 Fun_GetCol(string sCol)
{
return DBUtility.Xt_BaseBillFun.Fun_GetCol(sCol, grdmain);
}
}
}