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;
namespace BaseSet
{
public partial class Gy_DataInTmp_QCKC : Form
{
DAL.ClsGy_InitStock_Ctl oIS = new DAL.ClsGy_InitStock_Ctl();
DAL.ClsGy_Material_View oMater = new DAL.ClsGy_Material_View();
DAL.ClsGy_Unit_View oUnit = new DAL.ClsGy_Unit_View();
DAL.ClsGy_Warehouse_View oWH = new DAL.ClsGy_Warehouse_View();
DAL.ClsGy_StockPlace_View oSP = new DAL.ClsGy_StockPlace_View();
//
private string fileName = null; //ÎļþÃû
private IWorkbook workbook = null;
private FileStream fs = null;
private bool disposed;
string sHavSame = "";
//
int sYearCol = 0;
int sPeriodCol = 0;
int sHNumberCol = 0;
int sHWHNumberCol = 0;
int sHSPNumberCol = 0;
int sHUnitCol = 0;
int sQtyCol = 0;
int sMoneyCol = 0;
public Gy_DataInTmp_QCKC()
{
InitializeComponent();
}
private void initHeadCaption()
{
}
//
private void yl_Click(object sender, EventArgs e)
{
//WriteClass_ParentCode();
WriteClass();
}
//Êý¾Ýµ¼Èë
public void WriteClass()
{
sYearCol = DBUtility.Gy_BaseFun.Fun_GetCol("»á¼ÆÄê", grdmain);
sPeriodCol = DBUtility.Gy_BaseFun.Fun_GetCol("»á¼ÆÔÂ", grdmain);
sHNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏ´úÂë", grdmain);
sHWHNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("²Ö¿â´úÂë", grdmain);
sHSPNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("²Öλ´úÂë", grdmain);
sHUnitCol = DBUtility.Gy_BaseFun.Fun_GetCol("µ¥Î»", grdmain);
sQtyCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÊýÁ¿", grdmain);
sMoneyCol = DBUtility.Gy_BaseFun.Fun_GetCol("½ð¶î", grdmain);
if (sHNumberCol == -1)
{
MessageBox.Show("ûÓÐÕÒµ½¡¾ÎïÁÏ´úÂë¡¿µÄ±êÌâ", "Ìáʾ");
return;
}
if (sHWHNumberCol == -1)
{
MessageBox.Show("ûÓÐÕÒµ½¡¾²Ö¿â´úÂë¡¿µÄ±êÌâ", "Ìáʾ");
return;
}
if (sHSPNumberCol == -1)
{
MessageBox.Show("ûÓÐÕÒµ½¡¾²Öλ´úÂë¡¿µÄ±êÌâ", "Ìáʾ");
return;
}
if (sHUnitCol == -1)
{
MessageBox.Show("ûÓÐÕÒµ½¡¾µ¥Î»¡¿µÄ±êÌâ", "Ìáʾ");
return;
}
if (sQtyCol == -1)
{
MessageBox.Show("ûÓÐÕÒµ½¡¾ÊýÁ¿¡¿µÄ±êÌâ", "Ìáʾ");
return;
}
if (sMoneyCol == -1)
{
MessageBox.Show("ûÓÐÕÒµ½¡¾½ð¶î¡¿µÄ±êÌâ", "Ìáʾ");
return;
}
sHavSame = "";
int sNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏ´úÂë", grdmain);
for (int i = 0; i <= grdmain.Rows.Count - 1; i++)
{
if (DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sNumberCol].Value) != "")
{
if (Mater_WriteClass(i))
{
oIS.AddNew();
}
}
}
MessageBox.Show("µ¼ÈëÍê±Ï");
grdmain.DataSource = null;
}
//дÈëÀà ´Ó¿Ø¼þ
private bool Mater_WriteClass(int row)
{
//
string sHNumber = "";
long HItemID = 0;
//
sHNumber = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHNumberCol].Value);
//ÅжÏÊÇ·ñÔÊÐí ±£´æ
//дÈëÐÅÏ¢
oIS.oModel.HYear = DBUtility.ClsPub.isLong(grdmain.Rows[row].Cells[sYearCol].Value);
oIS.oModel.HPeriod = DBUtility.ClsPub.isLong(grdmain.Rows[row].Cells[sPeriodCol].Value);
if (oWH.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHWHNumberCol].Value)))
{
oIS.oModel.HWHID = oWH.omodel.HItemID;
}
//
if (oSP.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHSPNumberCol].Value)))
{
oIS.oModel.HSpID = oSP.omodel.HItemID;
}
//
if (oMater.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHNumberCol].Value)))
{
oIS.oModel.HMaterID = oMater.omodel.HItemID;
}
//
if (oUnit.GetInfoByName(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHUnitCol].Value)))
{
oIS.oModel.HUnitID = oUnit.omodel.HItemID;
}
oIS.oModel.HBatchNo = "";
oIS.oModel.HBeginQty = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sQtyCol].Value);
oIS.oModel.HBeginBal = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sMoneyCol].Value);
oIS.oModel.HReceiveQty =0;
oIS.oModel.HSendQty = 0;
oIS.oModel.HEndQty = 0;
oIS.oModel.HReceiveBal = 0;
oIS.oModel.HSendBal = 0;
oIS.oModel.HEndBal = 0;
oIS.oModel.HRemark = "";
return true;
}
private void tc_Click(object sender, EventArgs e)
{
this.Close();
}
private void K3_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() == "")
{
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);
grdmain.DataSource = d.DefaultView;
}
else
{
ExcelPath = "";
}
}
}
}