RFID的winform程序心得

RFID的winform程序心得

RFID的winform程序心得1-LMLPHP

 /// 根据日期获取当周星期一
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static DateTime StartOfTheWeek(DateTime dt)
{
return dt.AddDays(-(Convert.ToInt16(dt.DayOfWeek)-));
}
/// <summary>
/// 根据日期获取当周末
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static DateTime EndOfTheWeek(DateTime dt)
{
return StartOfTheWeek(dt).AddDays();
}
/// <summary>
///根据日期获取月初日期
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static DateTime StartOfTheMonth(DateTime dt)
{
return new DateTime(dt.Year, dt.Month, );
}
/// <summary>
/// 根据日期获取月底日期
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static DateTime EndOfTheMonth(DateTime dt)
{
return StartOfTheMonth(dt).AddMonths().AddDays(-);
}
/// <summary>
/// 感觉日期年初日期
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static DateTime StartOfTheYear(DateTime dt)
{
return new DateTime(dt.Year, , );
} /// <summary>
/// 根据日期获取年底日期
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static DateTime EndOfTheYear(DateTime dt)
{
return StartOfTheYear(dt).AddYears().AddDays(-);
}
/**//// <summary>
/// 常用方法,列之间加\t,一行一行输出,此文件其实是csv文件,不过默认可以当成Excel打开。
/// </summary>
/// <remarks>
/// using System.IO;
/// </remarks>
/// <param name="dgv"></param>
private void DataGridViewToExcel(DataGridView dgv)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Execl files (*.xls)|*.xls";
dlg.FilterIndex = ;
dlg.RestoreDirectory = true;
dlg.CreatePrompt = true;
dlg.Title = "保存为Excel文件"; if (dlg.ShowDialog() == DialogResult.OK)
{
Stream myStream;
myStream = dlg.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-));
string columnTitle = "";
try
{
//写入列标题
for (int i = ; i < dgv.ColumnCount; i++)
{
if (i > )
{
columnTitle += "\t";
}
columnTitle += dgv.Columns[i].HeaderText;
}
sw.WriteLine(columnTitle); //写入列内容
for (int j = ; j < dgv.Rows.Count; j++)
{
string columnValue = "";
for (int k = ; k < dgv.Columns.Count; k++)
{
if (k > )
{
columnValue += "\t";
}
if (dgv.Rows[j].Cells[k].Value == null)
columnValue += "";
else
columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
}
sw.WriteLine(columnValue);
}
sw.Close();
myStream.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
}
 /// <summary>
/// 将DataGridView中的数据导入到Excel中
/// </summary>
/// <param name="dt">DataGridView</param>
/// <param name="templatePath">模板的路径</param>
/// <param name="excelName">导入Excel的路径</param>
/// <returns></returns>
public static bool DataGridViewToExcel(DataGridView dg, string templatePath, string excelName)
{
bool result = true;
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(templatePath))
{
xmlDoc.Load(templatePath);
XmlNode root = xmlDoc.SelectSingleNode(@"/*[local-name()='Workbook']/*[local-name()='Worksheet'][1]/*[local-name()='Table']");
foreach (DataGridViewRow dgRow in dg.Rows)
{
XmlElement row = xmlDoc.CreateElement("", "Row", "urn:schemas-microsoft-com:office:spreadsheet");
row.SetAttribute("AutoFitHeight", "urn:schemas-microsoft-com:office:spreadsheet", "");
foreach (DataGridViewColumn dgcol in dg.Columns)
{
if (dgcol.Visible && dgcol.IsDataBound && dgcol.GetType().Name == "DataGridViewTextBoxColumn")
{
XmlElement cell = xmlDoc.CreateElement("", "Cell", "urn:schemas-microsoft-com:office:spreadsheet");
XmlElement data = xmlDoc.CreateElement("", "Data", "urn:schemas-microsoft-com:office:spreadsheet");
data.SetAttribute("Type", "urn:schemas-microsoft-com:office:spreadsheet", "String");
data.InnerText = StringUility.ReturnFormatString(dgRow.Cells[dgcol.Name].Value);
cell.AppendChild(data);
row.AppendChild(cell);
}
}
//root.InsertBefore(row, root.LastChild);
root.AppendChild(row);
} #region 添加合计行
XmlElement totalRow = xmlDoc.CreateElement("", "Row", "urn:schemas-microsoft-com:office:spreadsheet");
totalRow.SetAttribute("AutoFitHeight", "urn:schemas-microsoft-com:office:spreadsheet", "");
for (int i = ; i < ; i++)
{
XmlElement cell = xmlDoc.CreateElement("", "Cell", "urn:schemas-microsoft-com:office:spreadsheet");
XmlElement data = xmlDoc.CreateElement("", "Data", "urn:schemas-microsoft-com:office:spreadsheet");
data.SetAttribute("Type", "urn:schemas-microsoft-com:office:spreadsheet", i == ? "String" : "Number");
data.InnerText =(i==?"合计":dg.Rows.Count.ToString());
cell.AppendChild(data);
totalRow.AppendChild(cell);
}
root.AppendChild(totalRow);
#endregion XmlNode countnode = xmlDoc.SelectSingleNode(@"/*[local-name()='Workbook']/*[local-name()='Worksheet'][1]/*[local-name()='Table']");
countnode.Attributes["ss:ExpandedRowCount"].Value = (Int32.Parse(countnode.Attributes["ss:ExpandedRowCount"].Value) + dg.Rows.Count+).ToString();//标题+合计+数据
xmlDoc.Save(excelName);
result = true;
}
else
{
result = false;
}
return result;
}

RFID的winform程序心得2

05-11 15:21