C#订单统计小程序
发布日期:2021-05-10 12:00:46 浏览次数:21 分类:精选文章

本文共 3943 字,大约阅读时间需要 13 分钟。

������Excel������������

1.1 ���������sheet���������������

������������������������������������

  • ���������������������������������������������
  • ������������������������������sheet���������

1.2 ���������sheet���������������

������������������������������

  • ������������������������������

1.3 ���������������sheet���������������

  • ������������������������������������������������A������
  • ������������������������

������WinForm������

WinForm���������������

  • ������������������������������������
    • ���������������������������
    • ���������������������������������������������������������
    • ������������������������������������������

������������������

3.1 ������������������

private void openFileBtn_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
File_Address_textBox1.Text = openFileDialog1.FileName;
}
}

3.2 ������������������

private void saveFileBtn_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
File_Address_textBox2.Text = saveFileDialog1.FileName;
}
}

3.3 ������������������

private void beginBtn_Click(object sender, EventArgs e)
{
// 1. ������������������
string constr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + File_Address_textBox1.Text + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
OleDbConnection myConn = new OleDbConnection(constr);
// 2. ������������
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", myConn);
OleDbDataAdapter db = new OleDbDataAdapter("SELECT * FROM [������$]", myConn);
DataSet ds = new DataSet();
DataSet ds2 = new DataSet();
da.Fill(ds, "[Sheet1$]");
db.Fill(ds2, "[������$]");
myConn.Close();
// 3. ������������������
List
lists = new List
();
foreach (DataRow d in ds.Tables[0].Rows)
{
orders item = new orders();
string name = d.ItemArray[1].ToString();
if (name == "")
{
continue;
}
int num = int.Parse(d.ItemArray[2].ToString());
foreach (orders temp in lists)
{
if (temp.name == name)
{
temp.num += num;
break;
}
}
if (!lists.Any(t => t.name == name))
{
item.name = name;
item.num = num;
lists.Add(item);
}
}
// 4. ������������������
foreach (DataRow d in ds2.Tables[0].Rows)
{
string name = d.ItemArray[0].ToString();
float price = float.Parse(d.ItemArray[1].ToString());
foreach (orders temp in lists)
{
if (temp.name == name)
{
temp.totle = temp.num * price;
}
}
}
saveExcelFile(lists);
}

3.4 ������Excel������

private void saveExcelFile(List
lists)
{
StreamWriter strmWriterObj = new StreamWriter(File_Address_textBox2.Text, false, Encoding.UTF8);
strmWriterObj.WriteLine("������,������,������,������");
foreach (orders temp in lists)
{
strmWriterObj.WriteLine(temp.name + "," + temp.num + "," + temp.price + "," + temp.totle);
}
strmWriterObj.WriteLine("���������," + GetTotalNum(lists) + ", , " + GetTotalPrice(lists));
strmWriterObj.Close();
MessageBox.Show("������������");
}

���������������������

public class orders
{
public string name { get; set; } = "";
public int num { get; set; } = 0;
public float price { get; set; } = 0;
public float totle { get; set; } = 0;
}

������������������

private static int GetTotalNum(List
lists)
{
int total = 0;
foreach (orders temp in lists)
{
total += temp.num;
}
return total;
}
private static float GetTotalPrice(List
lists)
{
float total = 0;
foreach (orders temp in lists)
{
total += temp.totle;
}
return total;
}
上一篇:一张图搞定交换机路由权限问题
下一篇:木马开发的基本理论基础 (六)

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月27日 18时18分36秒