当前位置:网站首页>【C#笔记】winform中保存DataGridView中的数据为Excel和CSV
【C#笔记】winform中保存DataGridView中的数据为Excel和CSV
2022-07-02 06:26:00 【寂云萧】
保存为Excel
使用system.io保存,需要导入的库:using System.IO;
//使用system.io保存
//grid为整个DataGridView对象,而filePath为要保存的路径(包括后缀名),下同
public void saveExcelIO(DataGridView grid, string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
StringBuilder sb = new StringBuilder();
//写入标题
for(int k = 0; k < grid.Columns.Count; k++)
{
if (grid.Columns[k].Visible)
{
//\t相当于是在键盘上打一个tab键
sb.Append(grid.Columns[k].HeaderText.ToString().Trim() + "\t");
}
}
//换行
sb.Append(Environment.NewLine);
//开始写入每行的数值
for (int i = 0; i < grid.Rows.Count - 1; i++)
{
System.Windows.Forms.Application.DoEvents();
for(int j = 0; j < grid.Columns.Count; j++)
{
if (grid.Columns[j].Visible)
{
//单元格有一定字节的数量限制,可能会超出,超出会出现两个单元格内容一样的情况
sb.Append(grid.Rows[i].Cells[j].Value.ToString().Trim() + "\t");
}
}
sb.Append(Environment.NewLine);//换行
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
}
保存为CSV
//保存csv
public void saveCSV(DataGridView grid,string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
string strLine = "";
//表头
for (int i = 0; i < grid.ColumnCount; i++)
{
if (i > 0)
strLine += ",";
strLine += grid.Columns[i].HeaderText;
}
strLine.Remove(strLine.Length - 1);
sw.WriteLine(strLine);
strLine = "";
for(int j = 0; j < grid.Rows.Count; j++)
{
strLine = "";
int col = grid.Columns.Count;
for(int k = 0; k < col; k++)
{
if (k > 0 && k < col)
strLine += ",";
if (grid.Rows[j].Cells[k].Value == null)
strLine += "";
else
{
string cell = grid.Rows[j].Cells[k].Value.ToString().Trim();
//防止里面有特殊符号
cell = cell.Replace("\"", "\"\"");
cell = "\"" + cell + "\"";
strLine += cell;
}
}
sw.WriteLine(strLine);
}
sw.Close();
}
边栏推荐
- One book 1078: sum of fractional sequences
- Proof and understanding of pointnet principle
- Mmdetection model fine tuning
- 【双目视觉】双目立体匹配
- What if the laptop task manager is gray and unavailable
- latex公式正体和斜体
- 【MobileNet V3】《Searching for MobileNetV3》
- [torch] some ideas to solve the problem that the tensor parameters have gradients and the weight is not updated
- How to turn on night mode on laptop
- 【多模态】CLIP模型
猜你喜欢

【TCDCN】《Facial landmark detection by deep multi-task learning》
![[introduction to information retrieval] Chapter 6 term weight and vector space model](/img/42/bc54da40a878198118648291e2e762.png)
[introduction to information retrieval] Chapter 6 term weight and vector space model

【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》

常见的机器学习相关评价指标

Memory model of program

Label propagation

The difference and understanding between generative model and discriminant model

win10+vs2017+denseflow编译

图片数据爬取工具Image-Downloader的安装和使用

Installation and use of image data crawling tool Image Downloader
随机推荐
PointNet原理证明与理解
【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》
[paper introduction] r-drop: regulated dropout for neural networks
latex公式正体和斜体
Implementation of yolov5 single image detection based on onnxruntime
Faster-ILOD、maskrcnn_benchmark安装过程及遇到问题
[CVPR‘22 Oral2] TAN: Temporal Alignment Networks for Long-term Video
机器学习理论学习:感知机
【Mixup】《Mixup:Beyond Empirical Risk Minimization》
MoCO ——Momentum Contrast for Unsupervised Visual Representation Learning
What if the laptop can't search the wireless network signal
Huawei machine test questions
【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》
Using MATLAB to realize: power method, inverse power method (origin displacement)
Faster-ILOD、maskrcnn_ Benchmark trains its own VOC data set and problem summary
[tricks] whiteningbert: an easy unsupervised sentence embedding approach
Mmdetection model fine tuning
CPU的寄存器
mmdetection训练自己的数据集--CVAT标注文件导出coco格式及相关操作
【Programming】