0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cscの作法 その470

Last updated at Posted at 2024-04-02

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

小遣い帳を、エクセルで出力せよ。

写真

image.png

サンプルコード


using System;
using System.Text;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Transactions;
using System.Data.SQLite;
using ClosedXML.Excel;

namespace App
{
	class Program {
		static void Main(string[] args) {
			var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Book3.xlsx");
			var wb = new XLWorkbook();
			var ws = wb.Worksheets.Add("Sheet1");
			int j = 1;
			ws.Cell(j, 1).Value = "日付";
			ws.Cell(j, 2).Value = "内訳";
			ws.Cell(j, 3).Value = "貰ったお金";
			ws.Cell(j, 4).Value = "使ったお金";
			ws.Cell(j, 5).Value = "残金";
			SQLiteConnectionStringBuilder ConnectionStr = new SQLiteConnectionStringBuilder();
			ConnectionStr.DataSource = "db0.sqlite";
			using (SQLiteConnection Connection = new SQLiteConnection(ConnectionStr.ToString()))
			{
				Connection.Open();
				using(SQLiteCommand Command = new SQLiteCommand(Connection))
				{
					Command.CommandText = "SELECT * From kozu ORDER BY adate ASC";
					using(SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(Command))
					{
						DataTable dt = new DataTable();
						dataAdapter.Fill(dt);
						int pre = 0;
						for (int i = 0; i < dt.Rows.Count; i++)
						{
							j++;
							DataRow row = dt.Rows[i];
							ws.Cell(j, 1).Value = row["adate"];
							ws.Cell(j, 2).Value = row["memo"];
							ws.Cell(j, 3).Value = row["inp"];
							ws.Cell(j, 4).Value = row["oup"];
							int inp = 0;
							int oup = 0;
							int zan = 0;
							String s2 = Convert.ToString(row["inp"]);
							if (s2 != "")
							{
								inp = Convert.ToInt32(s2);
							}
							String s3 = Convert.ToString(row["oup"]);
							if (s3 != "")
							{
								oup = Convert.ToInt32(s3);
							}
							pre = inp - oup + pre;
							ws.Cell(j, 5).Value = pre;
						}
					}
				}
			}
			wb.SaveAs(filePath);
			Console.WriteLine("ok");
		}
	}
}






以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?